【问题标题】:Linear Layout does not fill the whole screen线性布局不会填满整个屏幕
【发布时间】:2017-01-25 03:28:09
【问题描述】:


为什么线性布局没有填满整个屏幕?
如您所见,我在线性布局中使用匹配父级。
如果我删除滚动条没有任何改变。该按钮不在屏幕底部。
ma​​in_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    tools:context="com.sarahp.demo.MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <TextView
                android:id="@+id/textview_demo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginBottom="32dp"
                android:layout_marginTop="32dp"
                android:text="@string/demo"
                android:textSize="42dp"/>

            <RelativeLayout
                android:id="@+id/fragment_container_a"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

            <RelativeLayout
                android:id="@+id/fragment_container_b"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_alignParentBottom="true"
                android:background="@color/transparent"/>

        </LinearLayout>
    </ScrollView>    
</RelativeLayout>

F A 是片段部分。

【问题讨论】:

  • 尝试给fragment_container_a,height = 0 and weight = 1
  • 底部还有空格
  • 你想要怎样的布局?

标签: android xml layout fragment


【解决方案1】:

android:fillViewport="true" 分配给您的 ScrollView。

您的 xml 将如下所示。 Main_Activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">


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


        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="Test Demo"
            android:textStyle="bold"
            android:gravity="center"/>

        <RelativeLayout
            android:id="@+id/fragment1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5"
            android:background="@drawable/border_image">
             <TextView
                 android:layout_width="match_parent"
                 android:layout_height="100dp"
                 android:gravity="center"
                 android:text="Test text Fragement1" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/fragment2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5"
            >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:gravity="center"
                android:text="Test text Fragement2" />
        </RelativeLayout>

    </LinearLayout>
</ScrollView>
</RelativeLayout>

【讨论】:

    【解决方案2】:

    移除相关布局

    <ScrollView
    
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <LinearLayout                                
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    
                <TextView
                    android:id="@+id/textview_demo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginBottom="32dp"
                    android:layout_marginTop="32dp"
                    android:text="@string/demo"
                    android:textSize="42dp"/>
    
                <RelativeLayout
                    android:id="@+id/fragment_container_a"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
    
                <RelativeLayout
                    android:id="@+id/fragment_container_b"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_alignParentBottom="true"
                    android:background="@color/transparent"/>
    
            </LinearLayout>
        </ScrollView> 
    

    【讨论】:

      猜你喜欢
      • 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
      相关资源
      最近更新 更多