简体   繁体   English

Android ScrollView 在底部被切断

[英]Android ScrollView gets cut off at the bottom

I am trying to programmatically add form fields picked from an HTML file to a LinearLayout.我正在尝试以编程方式将从 HTML 文件中选取的表单字段添加到 LinearLayout。 I have a next button at the bottom but it keeps getting cut off in the display.我在底部有一个下一步按钮,但它一直在显示中被切断。 I tried it on a tablet and it still doesnt show up.我在平板电脑上试了一下,还是不显示。

Here's a screenshot of the app:这是该应用程序的屏幕截图: 截图

As you can see, the elements are getting rendered but the last one runs off the screen for some reason.如您所见,元素正在渲染,但由于某种原因,最后一个元素从屏幕上消失了。

Fragment's XML:片段的 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>

Calling Activity's XML:调用 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>

I am calling a method formInflator that I made in the fragment's onCreateView and passing the LinearLayout from the fragment and an Elements object (from Jsoup library) which contains all the Elements that I want to put inside the LinearLayout:我正在调用我在片段的onCreateView创建的方法formInflator并传递来自片段的 LinearLayout 和一个Elements对象(来自 Jsoup 库),其中包含我想要放入 LinearLayout 的所有元素:

@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;

}

Here's the method formInflator :这是方法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;
    }
}

Any idea what I am doing wrong?知道我做错了什么吗?

Change your ScrollView layout_height to match_parent .将您的ScrollView layout_height 更改为match_parent It's the child that gets wrap_content .这是获得wrap_content的孩子。

Try the Nested ScrollView.尝试嵌套滚动视图。 It did wonders for me.它为我创造了奇迹。

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

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

The most close solution that I have ever found is adding a ghost view... If the ScrollView is eating part of the last element, give it a cookie.我找到的最接近的解决方案是添加一个幽灵视图......如果 ScrollView 正在吃最后一个元素的一部分,给它一个 cookie。

Ex:例如:

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

Here's how i figured out how to do it without padding.这是我如何在没有填充的情况下弄清楚如何做到这一点。 The scrollview gets perfectly to the bottom on every screen size and orientation I have tried.在我尝试过的每个屏幕尺寸和方向上,滚动视图都会完美地到达底部。 Notice the height is 0dp and there is a constraint to the bottom of parent.请注意,高度为 0dp,并且对父级底部有约束。

<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>

Add your action bar to the activity.将您的操作栏添加到活动中。 Android will then better recognise the view port and add the missing toolbar height on the bottom, when you scroll.然后,当您滚动时,Android 将更好地识别视口并在底部添加缺少的工具栏高度。

AppCompatActivity应用兼容活动

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM