ScrollView布局未填满整个屏幕

86
我有一个包含两个Fragment(一个列表和一个普通的)的Activity。普通的Fragment中填充了一个包含TextViews的垂直LinearLayout的ScrollView。ScrollView和layout_width以及layout_height都是match_parent,因此我认为整个屏幕都应该被使用。但是底部仍然存在一个“间隙”。我希望你能帮助我。

ScrollView.xml

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

<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/titlescreen_bg"
    android:orientation="vertical"
    android:paddingTop="60dp"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tv_headline"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingBottom="60dp"
        android:paddingTop="60dp"
        android:textIsSelectable="false"
        android:textSize="@dimen/fontsize_slogan_titlescreen" />

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:paddingBottom="30dp"
        android:paddingTop="30dp"
        android:textIsSelectable="false"
        android:textSize="@dimen/fontsize_slogan_titlescreen" />
</LinearLayout>

</ScrollView>

充气此布局的片段。

package wak.iage.layout;

import wak.iage.R;
import android.app.Fragment;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MenuContentFragment extends Fragment
{
LinearLayout.LayoutParams   relativeParams  = new LinearLayout.LayoutParams(
                                                    LayoutParams.MATCH_PARENT,
                                                    LayoutParams.MATCH_PARENT);
LinearLayout                topLayout       = null;
TextView                    body            = null;
TextView                    head            = null;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.menu_content_main, container);
    return v;
}

public void changeText(String title, String content) {
    topLayout = (LinearLayout) getActivity().findViewById(
            R.id.LinearLayout1);
    head = (TextView) getActivity().findViewById(R.id.tv_headline);
    body = (TextView) getActivity().findViewById(R.id.tv_content);

    if (body == null) {
        topLayout.removeViews(1, topLayout.getChildCount() - 1);
        body = new TextView(getActivity().getApplicationContext());
        body.setPadding(0, 30, 0, 20);
        body.setTextColor(Color.BLACK);
        body.setTextSize(22);
        body.setGravity(Gravity.CENTER_HORIZONTAL);
        topLayout.addView(body, relativeParams);
    }

    body.setText(content);
    head.setText(title);
}

public void addGlossary() {
    if (body != null) {
        topLayout.removeView(body);
    }

    int i = 0;

    for (int id : GLOSSARY) {
        TextView glossary = new TextView(getActivity()
                .getApplicationContext());
        glossary.setText(getString(id));
        glossary.setTextColor(Color.BLACK);
        if (i % 2 == 0) {
            glossary.setTypeface(Typeface.DEFAULT_BOLD);
            glossary.setTextSize(22);
            glossary.setPadding(0, 10, 0, 10);
        }
        topLayout.addView(glossary, relativeParams);
        i += 1;
    }
}

public static final int[]   GLOSSARY    = {
        R.string.GlossaryAndroidOSTitle, R.string.GlossaryAndroidOSContent,
        R.string.GlossaryAppTitle, R.string.GlossaryAppContent,
        R.string.GlossaryCloudTitle, R.string.GlossaryCloudContent,
        R.string.GlossaryDonwloadTitle, R.string.GlossaryDonwloadContent,
        R.string.GlossaryFacebookTitle, R.string.GlossaryFacebookContent,
        R.string.GlossaryGPSTitle, R.string.GlossaryGPSContent,
        R.string.GlossaryHomescreenTitle,
        R.string.GlossaryHomescreenContent, R.string.GlossaryPasswordTitle,
        R.string.GlossaryPasswordContent, R.string.GlossaryRouterTitle,
        R.string.GlossaryRouterContent, R.string.GlossarySDTitle,
        R.string.GlossaySDContent, R.string.GlossayStandbyTitle,
        R.string.GlossayStandbyContent, R.string.GlossaryTabletTitle,
        R.string.GlossaryTabletContent, R.string.GlossaryTouchscreenTitle,
        R.string.GlossaryTouchscreenContent, R.string.GlossayWidgetsTitle,
        R.string.GlossayWidgetsContent, R.string.GlossayWLANTitle,
        R.string.GlossayWLANContent };
}

非常感谢。

编辑:即使问题已经通过android:fillViewPort="true"修复,我还是想向您展示问题。

但我没有足够的声望来发布图片。抱歉!


1
你能发一张截图吗? - FoamyGuy
5个回答

310
如果我没记错的话,ViewGroup的高度(在您的情况下为LinearLayout的高度),它是ScrollView中唯一的子元素,总是被解释为wrap_content,因为该内容可能比ScrollView的高度更大(因此出现滚动条)。
这也意味着,如果内容较小,则ScrollView的内容(子元素)不一定会拉伸以填充屏幕。
为了在视觉上帮助您解决此问题,我们需要看到您的问题截图。
也许在ScrollView上设置android:fillViewport="true"可以解决您的问题:
<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">

19
fillViewPort解决了问题。谢谢! - j0chn
1
关于“ScrollView中的(唯一)子项始终被解释为wrap_content,因为该内容可能比ScrollView的高度更大(因此出现滚动条)”:您能指向执行此操作的源代码吗? - user1443317
在我的情况下,子LinearLayout有足够的内容超出了屏幕空间,但是仍然没有填满整个屏幕,底部留有大约10%的空白。使用fillViewPort解决了这个问题。我从昨天开始一直在折腾这段代码,你真是救星啊! - T.M
fillViewport="true" 是一个很棒的功能,可以将滚动视图拉伸到整个屏幕高度,并使内容在滚动视图中居中。 - Michał Ziobro
android:layout_height="match_parent" should be android:layout_height="wrap_content" - Jimit Patel

11
<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" >
在你的ScrollView中添加一个属性,例如:
android:fillViewport="true"

android:layout_height="fill_parent" should be android:layout_height="wrap_content" - Jimit Patel

2
inflater.inflate(R.layout.menu_content_main, container);

应该是

inflater.inflate(R.layout.menu_content_main, container, false);

1
不,这不是问题所在。 - j0chn

1
使用NestedScrollView替换ScrollView,这也会解决嵌套滚动的问题。
<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>

这个对我有效,在尝试了这里所有其他建议之后。 - bashizip

0

我曾经遇到过类似的问题,只能通过使用 Helper 类来解决。我在网上找到了原始代码,并进行了实现。

Java 类:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接