【问题标题】:ScrollView padding ignored on keyboard show在键盘显示上忽略 ScrollView 填充
【发布时间】:2020-02-27 16:10:25
【问题描述】:

假设我有这个简单的布局:

https://imgur.com/k6zuh3f

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="80dp"
            android:clipToPadding="false">

        <LinearLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="16dp">

            <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/ic_launcher" />

            ...


            <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Eighth" />

        </LinearLayout>

    </ScrollView>

    <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_margin="16dp"
            android:text="Button" />

</RelativeLayout>

注意paddingBottom 上有ScrollViewclipToPadding=false。这是必需的,所以Button 看起来像滚动视图内容上的浮动按钮。 ScrollView 填充用于在内容下方腾出空间以使最后一个子可用。

如果最后一个孩子是EditText,我希望ScrollView 滚动使EditText 在软件键盘上可见。但它以https://imgur.com/GwOtLIq结束了

使用layout_marginBottom 而不是paddingBottom 可以实现某种预期行为,但在这种情况下,我显然看不到Button 后面的内容。截图https://imgur.com/oSC24qV

有没有办法让ScrollView 在键盘避免方面尊重其填充?

更新:这里有完整的 xml 代码https://pastebin.com/P8n0aZ2i

【问题讨论】:

  • 尝试将填充添加到最后一个编辑文本而不是滚动视图
  • 为顶层视图提供 padding 并为线性布局提供 paddingBottom。它会工作
  • 如果我对EditText 使用填充,它的背景大小只会变得更大(默认材质主题下划线会更低),但没有帮助。
  • 使用协调器布局。
  • 小问题,打开软键盘后可以向上滚动吗?因为您使用相同的 xml 在顶部使用 ConstraintLayout 对我有用。打开软键盘后,我可以向上滚动显示按钮顶部的最后一个EditText

标签: android android-layout scrollview


【解决方案1】:

您只需要在清单中为您的活动定义软输入调整类型。 您可以在清单中添加以下行。

 <activity android:name=".Main2Activity" android:theme="@style/BaseTheme_FullScreen2"
              android:windowSoftInputMode="adjustResize|adjustPan">
    </activity>

【讨论】:

  • 不,它没有帮助。 adjustPan 将按钮留在键盘后面,内容不在键盘上方滚动。 adjustResize 保持不变 - imgur.com/GwOtLIq
【解决方案2】:

如果您想在键盘打开时实现滚动,您必须调整小部件以使它们不会相互冲突。

添加 android:layout_above="@+id/button"scrollview 小部件中,这样当键盘显示时它会在按钮上自行调整。

代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="80dp"
        android:layout_above="@+id/button"
        android:clipToPadding="false">

        <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

            ...


            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Eighth" />

        </LinearLayout>

    </ScrollView>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="16dp"
        android:text="Button" />

</RelativeLayout>

【讨论】:

  • 这否定了我这样做的全部意义。我想看看Button背后的内容。但我也想让 EditText 在键盘显示上可见。
【解决方案3】:

使用CoordinatorLayout 代替RelativeLayout,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<ScrollView
        android:id="@+id/scroll_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

        ...


        <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Eighth" />

    </LinearLayout>

</ScrollView>

<Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        app:layout_anchorGravity=”bottom|end”
        android:layout_margin="16dp"
        app:layout_anchor=”@id/scroll_container” 
        app:layout_dodgeInsetEdges="bottom"
        android:text="Button" />

</CoordinatorLayout>

我没有对此进行测试,但除了在清单中为活动标签设置android:windowSoftInputMode="adjustResize|adjustPan" 外,它应该可以按您的意愿工作。

【讨论】:

    【解决方案4】:

    我对android不太了解,但作为Java程序员,我想建议你在Activity类中改变你的scrollView大小。 请将您的 scrollView 放入其他一些 CordinatorLayout 并将下面的代码放入 init()onCreate() 方法中

     protected void onCreate(Bundle savedInstanceState) {
       
        mySwipeRefreshLayout.setOnRefreshListener(
                new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                      scrollView.setMinHeight(layoutComposite.computeSize(layoutComposite.getSize().x+100 , SWT.DEFAULT).y+100);
    				
                    }
                }
        );
    }

    在activity refresh/onload/onCreate中调用上面的代码

    Intent intent=new Intent(Current_Activity.this,Current_Activity.class);
        startActivity(intent);
        finish();
    

    参考这个链接 Scroll Implementation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-15
      • 2011-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 2011-04-24
      相关资源
      最近更新 更多