首页 > android LinearLayout剩余空间的问题。

android LinearLayout剩余空间的问题。

在LinearLayout中,使用

android:layout_weight="1"
android:layout_height="0dp"

这种写法是否可以将控件的高度设置为余下的所有空间?
为什么这段代码用在ListView和RecyclerView可以完美占据剩余空间,而在TextView中却不可以呢?
求教!
代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:weightSum="1">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/divider"
        android:background="@color/divide" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/app_notify_content_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/app_alert_tip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/common_item_padding"
                android:layout_marginEnd="@dimen/common_item_padding"
                android:layout_marginTop="26dp"
                android:layout_marginBottom="7.3dp"
                android:text="@string/app_notify_tips"
                android:textColor="@color/black40"
                android:textSize="11.3sp" />

            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/divider"
                android:layout_marginLeft="@dimen/common_item_padding"
                android:layout_marginRight="@dimen/common_item_padding"
                android:background="@color/divide" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="51dp">

                <TextView
                    android:id="@+id/app_switch_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15.3dp"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="23.3dp"
                    android:text="@string/enable_app_alert"
                    android:textColor="@color/black70"
                    android:textSize="14.7sp" />


                <Switch
                    android:id="@+id/app_notification_alert_switch"
                    android:layout_width="40dp"
                    android:layout_height="21dp"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="23.3dp"
                    android:visibility="visible"
                    style="@style/switchStyle"/>

            </RelativeLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/divider"
                android:layout_marginLeft="@dimen/common_item_padding"
                android:layout_marginRight="@dimen/common_item_padding"
                android:background="@color/divide" />

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

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

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="51dp">

                        <TextView
                            android:id="@+id/app_notifi_awake_title"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="15.3dp"
                            android:layout_alignParentLeft="true"
                            android:layout_marginLeft="23.3dp"
                            android:text="仅在熄屏时通知"
                            android:textColor="@color/black70"
                            android:textSize="14.7sp" />

                        <Switch
                            android:id="@+id/app_notifi_awake_switch"
                            android:layout_width="40dp"
                            android:layout_height="21dp"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:layout_marginRight="23.3dp"
                            style="@style/switchStyle" />

                    </RelativeLayout>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/divider"
                        android:layout_marginLeft="@dimen/common_item_padding"
                        android:layout_marginRight="@dimen/common_item_padding"
                        android:background="@color/divide" />


                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/common_item_padding"
                        android:layout_marginTop="26dp"
                        android:gravity="center_vertical"
                        android:text="@string/selected_alert_apps"
                        android:textColor="@color/black40"
                        android:textSize="11.3sp" />

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/divider"
                        android:layout_marginLeft="@dimen/common_item_padding"
                        android:layout_marginRight="@dimen/common_item_padding"
                        android:layout_marginTop="7.3dp"
                        android:background="@color/divide" />

                     <TextView
                        android:id="@+id/list_tip"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                         android:layout_weight="1"
                        android:gravity="center"
                        android:layout_gravity="center"
                        android:layout_marginTop="-15dp"
                        android:text="afasdfsdvsdfbsdfgbsdfhsghnsdfgsdfgasdgasdfgshnsgh"
                        android:textSize="14.7sp" />
                    
                    <FrameLayout
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/chart_bottom_bar_height"
                        android:orientation="vertical">

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="@dimen/divider"
                            android:background="@color/divide" />

                        <TextView
                            android:id="@+id/add_notification_app_btn"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:drawablePadding="4.7dp"
                            android:gravity="center"
                            android:drawableTop="@drawable/app_notify_edit_button_press"
                            android:layout_marginTop="2dp"
                            android:text="@string/edit_app"
                            android:textColor="@color/black40"
                            android:textSize="9.3sp" />
                    </FrameLayout>

                </LinearLayout>

                <View
                    android:id="@+id/mask_view1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="bottom"
                    android:clickable="true"
                    android:background="#77f7f7f7"
                    android:visibility="gone"/>

            </FrameLayout>


        </LinearLayout>

        <View
            android:id="@+id/mask_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="bottom"
            android:clickable="true"
            android:background="#77f7f7f7"
            android:visibility="gone"/>

    </FrameLayout>


</LinearLayout>

textview中也是可以的,是不是你的textview字体太小,显示在上方了


LinearLayout是有方向的,

`android:layout_weight="1"
android:layout_height="0dp"`

仅仅在垂直布局时占据剩余的空间,假如LinearLayout是横向的,那就不生效了
这样

`android:layout_weight="1"
    android:layout_width="0dp"`

贴一下你textview的整个布局文件


你将你的布局文件全部贴出来

【热门文章】
【热门文章】