ScrollView Layout does not fill the whole screen

后端 未结 5 2036
一整个雨季
一整个雨季 2021-01-31 06:54

I got an Activity with two Fragments (one list one normal). And the normal Fragment inflates a Scrollview containing a

相关标签:
5条回答
  • 2021-01-31 07:24

    I had a similar problem and could only fix it with a Helper Class. I found the original code online and this is my implementation of it.

    Java class:

    public class ImageViewHelper extends android.support.v7.widget.AppCompatImageView {
    
        public ImageViewHelper(Context context) {
            super(context);
        }
    
        public ImageViewHelper(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public ImageViewHelper(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            Drawable d = getDrawable();
            if (d != null) {
                int w = MeasureSpec.getSize(widthMeasureSpec);
                int h = w * d.getIntrinsicHeight() / d.getIntrinsicWidth();
                setMeasuredDimension(w, h);
            }
            else super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
    

    XML:

    <com.example.app.ImageViewHelper
        android:id="@+id/img"
        android:src="@drawable/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true" />
    
    0 讨论(0)
  • 2021-01-31 07:30
    inflater.inflate(R.layout.menu_content_main, container);
    

    should be

    inflater.inflate(R.layout.menu_content_main, container, false);
    
    0 讨论(0)
  • 2021-01-31 07:45

    If i'm not mistaken, the ViewGroup's height (LinearLayout's height in your case), that is the (only) child inside a ScrollView, is always interpreted as wrap_content, since that content can be larger than the ScrollView's height (hence the scrollbars).

    This also means that if the content is smaller, the ScrollView's content (child) may not necessarily stretch to fill the screen.

    In order to visually help you fix this, we need to see a screenshot of your problem.

    Maybe setting android:fillViewport="true" on the ScrollView will fix your issue:

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
    
    0 讨论(0)
  • 2021-01-31 07:47

    Replace ScrollView with NestedScrollView it will also solve the problem of nested scrolling.

    <androidx.core.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    </androidx.core.widget.NestedScrollView>
    
    0 讨论(0)
  • 2021-01-31 07:49
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"
        android:fadeScrollbars="false"
        android:scrollbars="vertical" >
    

    In your ScrollView add an attribute ie.

    android:fillViewport="true"
    
    0 讨论(0)
提交回复
热议问题