ScrollView布局不能填满整个屏幕 [英] ScrollView Layout does not fill the whole screen

查看:42
本文介绍了ScrollView布局不能填满整个屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个带有两个FragmentActivity(一个列出一个正常值). 普通Fragment将包含LineaLayout(垂直)的Scrollview膨胀,并且此布局包含TextViews. ScrollViewlayout_widthlayout_heightmatch_parent,所以我认为应该使用整个屏幕.但是在底部仍然有一个差距". 我希望你能帮助我.

I got an Activity with two Fragments (one list one normal). And the normal Fragment inflates a Scrollview containing a LineaLayout (vertical) and this layout contains TextViews. The ScrollView and layout_width and layout_height are match_parent, so I think the whole screen should be used. But on the bottom there is still a "gap". I hope you can help me.

ScrollView.xml

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>

使这种布局膨胀的片段.

the fragment inflating this layout.

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"修复,我想向您展示问题.

Even the proble is already fixed with: android:fillViewPort="true", I want to show you the problem.

但是我没有足够的声誉来发布图片. 抱歉!

But I don't have enough reputation to post a picture. Sorry!

推荐答案

如果我没记错的话,ViewGroup的高度(在您的情况下为LinearLayout的高度),即(唯一的)孩子ScrollView中的内容始终被解释为wrap_content,因为该内容可以大于ScrollView的高度(因此滚动条).

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

这也意味着,如果内容小于 ,则ScrollView的内容(子级)可能不一定会拉伸以填满整个屏幕.

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.

也许在ScrollView上设置android:fillViewport="true"可以解决您的问题:

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

这篇关于ScrollView布局不能填满整个屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆