【问题标题】:Android ScrollView gets cut off at the bottomAndroid ScrollView 在底部被截断
【发布时间】:2016-07-29 16:23:11
【问题描述】:

我正在尝试以编程方式将从 HTML 文件中挑选的表单字段添加到 LinearLayout。我在底部有一个下一步按钮,但它在显示屏中一直被切断。我在平板电脑上试了一下,还是不显示。

这是应用程序的屏幕截图:

如您所见,元素正在渲染,但最后一个元素由于某种原因从屏幕上消失了。

片段的 XML:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".dataInput.PropertyInfoFragment"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:paddingLeft="20dp"
    android:paddingRight="20dp">

    <ScrollView
        android:fillViewport="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/linear_layout_property_info"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

            </LinearLayout>
            <Button
                android:id="@+id/nextButton"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/next"
                android:background="@color/colorPrimary"
                android:textColor="@android:color/white"/>
        </LinearLayout>

    </ScrollView>

</FrameLayout>

调用Activity的XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".dataInput.DataInputActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay">

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_media_play" />

</android.support.design.widget.CoordinatorLayout>

我正在调用我在片段的onCreateView 中创建的方法formInflator 并从片段传递LinearLayout 和Elements 对象(来自Jsoup 库),其中包含我想要放入的所有元素线性布局:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_property_info, container, false);
    nextButton = (Button) view.findViewById(R.id.nextButton);
    nextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onButtonPressed();
        }
    });

    helpers.formInflator((LinearLayout) view.findViewById(R.id.linear_layout_property_info), generator.propertyTextElements);
    return view;

}

这是formInflator的方法:

public void formInflator(LinearLayout parentLayout, Elements formElements) {
    TextInputLayout index = null;
    for(Element textField : formElements) {
        TextInputEditText editText = new TextInputEditText(context);
        editText.setId(View.generateViewId());
        editText.setHint(textField.id());
        editText.setText(textField.text());
        LinearLayout.LayoutParams editTextParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        editText.setLayoutParams(editTextParams);

        TextInputLayout textInputLayout = new TextInputLayout(context);
        textInputLayout.setId(View.generateViewId());
        textInputLayout.setTag(textField.id());
        RelativeLayout.LayoutParams textInputLayoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        if (index == null)
            index = textInputLayout;
        else
            textInputLayoutParams.addRule(RelativeLayout.BELOW, index.getId());

        textInputLayout.setLayoutParams(textInputLayoutParams);
        textInputLayout.addView(editText, editTextParams);

        parentLayout.addView(textInputLayout, textInputLayoutParams);
        index = textInputLayout;
    }
}

知道我做错了什么吗?

【问题讨论】:

  • 如果它是一个滚动视图,您可以向上滚动查看其余的“截断”内容吗?您要达到的预期行为是什么?
  • @wanpanman 我想我没有正确地表达我的问题。问题是在所有 EditText 视图之后应该有一个按钮。但是 ScrollView 不会一直滚动到底部。我在屏幕截图中显示的是最远的。
  • 我有一种感觉是因为动作栏。我面临同样的问题。我正在使用选项卡式活动,在选项卡内我有EditTexts。最初,底部就像 OP 一样被切断。但是一旦我激活EditText 并且软键盘出现,操作栏就会消失(这是我似乎无法解决的另一个问题),但ScrollView 工作正常,即使在移除键盘后也是如此(操作栏仍然失踪)。
  • 如果您的 (Nested)ScrollViewConstraintLayout 内,您可能需要在滚动视图上设置 android:layout_height="0dp"(以及 app:layout_constraintTop_toBottomOfapp:layout_constraintBottom_toTopOf) - 如此处所述:stackoverflow.com/a/44843684/1617737

标签: android android-fragments android-linearlayout android-scrollview


【解决方案1】:

将您的 ScrollView layout_height 更改为 match_parent。得到wrap_content的是孩子。

【讨论】:

  • 谢谢,这绝对有帮助!然而它并没有完全解决问题。现在它滚动到最后一个EditText 视图的底部,但Button 仍然被切断。我尝试将Button 移动到第二个LinearLayout 上方,这导致最后一个EditText 再次被切断
  • formInflator 中,您尝试为添加到parentLayoutTextInputLayout 设置RelativeLayout 布局参数。但是parentLayoutLinearLayout,而不是RelativeLayout,所以这不可能达到你的预期。
【解决方案2】:

试试嵌套滚动视图。这对我来说确实很神奇。

<androidx.core.widget.NestedScrollView
    -----
    android:layout_width="match_parent"
android:layout_height="match_parent">

LinearLayout<
-----
</LinearLayout>
</androidx.core.widget.NestedScrollView>

【讨论】:

  • API 21 - 29 这个问题的精确解决方案。
  • 这个问题的完美解决方案,它按预期工作。
【解决方案3】:

我找到的最接近的解决方案是添加一个幽灵视图...如果 ScrollView 正在吃掉最后一个元素的一部分,请给它一个 cookie。

例如:

<View
    android:layout_width="match_parent"
    android:layout_height="25dp"/>

【讨论】:

  • 您可以简单地为滚动视图内的内容添加底部填充。
  • 没有人应该使用这个建议。即使显示内容,滚动视图仍然会在底部被切断...
【解决方案4】:

这就是我想出如何在没有填充的情况下做到这一点的方法。在我尝试过的每个屏幕尺寸和方向上,滚动视图都能完美地到达底部。请注意,高度为 0dp,并且对 parent 的底部有一个约束。

<ScrollView
    android:id="@+id/scrollView"
    style="@android:style/Widget.DeviceDefault.Light.ScrollView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/LLHeaders">

    <LinearLayout
        android:id="@+id/LLVisitorList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <My dynamically generated LinearLayout horizontal rows go here>

    </LinearLayout>
</ScrollView>

【讨论】:

    【解决方案5】:

    将您的操作栏添加到活动中。然后,当您滚动时,Android 将更好地识别视口并在底部添加缺少的工具栏高度。

    AppCompatActivity

    Toolbar toolbar = getActivity().findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // if needed...
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多