为什么我的LinearLayout会消失?

时间:2016-07-06 09:25:48

标签: android android-layout android-linearlayout

这是我的xml文件

 <?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"
    android:orientation="vertical">

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="testFun:"
            android:textColor="@color/text_black"
            android:textSize="@dimen/titleTextSize"
            android:textStyle="bold" />

    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/gray" />

    <RelativeLayout
        android:id="@+id/layout_currentStation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

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

            <View
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"></View>

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:singleLine="true"
                android:text="tv_station:"
                android:textColor="@color/text_black"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/tv_currentStation"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="6"
                android:background="@drawable/shape_home_white_r6"
                android:gravity="center_vertical"
                android:singleLine="true"
                android:textColor="@color/text_black"
                android:textColorHint="@color/text_gray"
                android:textSize="20sp" />

            <ImageView
                android:id="@+id/img_nomal_state_arrow"
                android:layout_width="30dp"
                android:layout_height="40dp"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="5dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:src="@drawable/icon_arrow_down" />

            <View
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"></View>
        </LinearLayout>

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/testLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="test" />
    </LinearLayout>

</LinearLayout>

我认为android系统会将剩余的空间分配给“testLayout”,因为它是唯一一个指定“layout_weight”属性,但结果是:

enter image description here

“testLayout”刚刚消失,为什么?

3 个答案:

答案 0 :(得分:2)

在相对布局中更改View的高度(在dp中使用修复高度)。

<RelativeLayout
        android:id="@+id/layout_currentStation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

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

            <View
                android:layout_width="0dp"
                android:layout_height="5dp"   // fix it in dp
                android:layout_weight="1"></View>

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:singleLine="true"
                android:text="tv_station:"
                android:textColor="@color/text_black"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/tv_currentStation"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="6"
                android:background="@drawable/shape_home_white_r6"
                android:gravity="center_vertical"
                android:singleLine="true"
                android:textColor="@color/text_black"
                android:textColorHint="@color/text_gray"
                android:textSize="20sp" />

            <ImageView
                android:id="@+id/img_nomal_state_arrow"
                android:layout_width="30dp"
                android:layout_height="40dp"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="5dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:src="@drawable/icon_arrow_down" />

            <View
                android:layout_width="0dp"
                android:layout_height="5dp"   // fix it in dp
                android:layout_weight="1"></View>
        </LinearLayout>

    </RelativeLayout>
希望它有所帮助。

答案 1 :(得分:0)

testLayout中没有方向,尝试向其添加垂直或水平方向

修改

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="testFun:"
        android:textColor="@color/text_black"
        android:textSize="@dimen/titleTextSize"
        android:textStyle="bold" />

</RelativeLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="@color/gray" />

<RelativeLayout
    android:id="@+id/layout_currentStation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp">

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

        <View
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"></View>

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:singleLine="true"
            android:text="tv_station:"
            android:textColor="@color/text_black"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/tv_currentStation"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="6"
            android:background="@drawable/shape_home_white_r6"
            android:gravity="center_vertical"
            android:singleLine="true"
            android:textColor="@color/text_black"
            android:textColorHint="@color/text_gray"
            android:textSize="20sp" />

        <ImageView
            android:id="@+id/img_nomal_state_arrow"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:src="@drawable/icon_arrow_down" />

        <View
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"></View>
    </LinearLayout>

</RelativeLayout>


<LinearLayout
    android:id="@+id/testLayout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="test" />
</LinearLayout>

我将Views高度设置为40dp,与TextView高度相同,现在“testLayout”在底部可见

答案 2 :(得分:0)

<?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"
android:orientation="vertical">

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="testFun:"
        android:textColor="@color/text_black"
        android:textSize="@dimen/titleTextSize"
        android:textStyle="bold" />

</RelativeLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="@color/gray" />

<RelativeLayout
    android:id="@+id/layout_currentStation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp">

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

        <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"></View>

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:singleLine="true"
            android:text="tv_station:"
            android:textColor="@color/text_black"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/tv_currentStation"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="6"
            android:background="@drawable/shape_home_white_r6"
            android:gravity="center_vertical"
            android:singleLine="true"
            android:textColor="@color/text_black"
            android:textColorHint="@color/text_gray"
            android:textSize="20sp" />

        <ImageView
            android:id="@+id/img_nomal_state_arrow"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:src="@drawable/icon_arrow_down" />

        <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"></View>
    </LinearLayout>

</RelativeLayout>

<LinearLayout
    android:id="@+id/testLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="test" />
</LinearLayout>