LinearLayout在RelativeLayout中的高度为0

时间:2017-05-15 09:23:24

标签: android xml android-recyclerview android-linearlayout relativelayout

我有RecyclerView项目

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_marginLeft="@dimen/default_padding"
    android:layout_marginRight="@dimen/default_padding"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

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

            <View
                android:layout_weight="1"
                android:background="@color/divider"
                android:layout_marginLeft="@dimen/bonus_circle_line_padding"
                android:layout_height="match_parent"
                android:layout_width="1dp"
                android:id="@+id/devider_top" />

            <View
                android:layout_weight="1"
                android:background="@color/divider"
                android:layout_marginLeft="@dimen/bonus_circle_line_padding"
                android:layout_height="match_parent"
                android:layout_width="1dp"
                android:id="@+id/devider_bottom" />

        </LinearLayout>

        <RelativeLayout
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:layout_centerInParent="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <bank.ui.view.FlexibleСircleText
                android:id="@+id/value_text"
                android:layout_width="@dimen/bonus_circle_text_max_diameter"
                android:layout_height="@dimen/bonus_circle_text_max_diameter"/>

            <LinearLayout
                android:orientation="vertical"
                android:layout_marginLeft="@dimen/default_padding"
                android:layout_marginRight="@dimen/default_padding"
                android:layout_width="match_parent"
                android:minHeight="@dimen/bonus_circle_text_max_diameter"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/value_text"
                android:layout_toEndOf="@+id/value_text"
                android:id="@+id/linearLayout2">

                <TextView
                    android:textSize="@dimen/font_size_standard"
                    android:text="text"
                    android:id="@+id/text"
                    android:layout_marginBottom="2dp"
                    android:gravity="bottom"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

                <TextView
                    android:textColor="@color/text_grey"
                    android:textSize="@dimen/font_size_small"
                    android:text="text"
                    android:id="@+id/amount"
                    android:layout_marginTop="2dp"
                    android:gravity="top"
                    android:layout_weight="1"
                    android:ellipsize="end"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

            </LinearLayout>

        </RelativeLayout>

    </RelativeLayout>

</LinearLayout>

项目的所有元素都有正常大小,但ID为“deviders_layout”的布局总是有height = 0;

我尝试添加

android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"

以id“deviders_layout”进行布局。这不适合我。

设计

enter image description here

2 个答案:

答案 0 :(得分:0)

标识为RelativeLayout

item_layout高度设置为wrap_content,它有2个子布局:

  • RelativeLayout,身高设置为wrap_content,内容高度不为空

  • 标识为LinearLayout的{​​{1}}的高度设置为deviders_layout,内容也设置为match_parent。使用此配置,您的布局将始终为&#34;扁平&#34;,您需要为内容指定高度并使用match_parent或将父 s 高度设置为wrap_content

答案 1 :(得分:0)

试试这个:

    <?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="wrap_content"
                android:background="#ffffff"
                android:orientation="vertical"
                android:paddingLeft="10dp">

                <TextView
                    android:id="@+id/value_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:textSize="16dp"
                    android:background="@drawable/txt_shape_bg"
                    android:padding="10dp"
                    android:text="200" />

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/value_text"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="0.5dp"
                    android:background="@color/play_game_controls_background_color"
                    android:paddingLeft="1dp">

                    <LinearLayout
                        android:id="@+id/linearLayout2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:background="#ffffff"
                        android:orientation="vertical"
                        android:paddingBottom="10dp"
                        android:paddingLeft="10dp">

                        <TextView
                            android:id="@+id/text"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_marginBottom="2dp"
                            android:layout_weight="1"
                            android:gravity="bottom"
                            android:text="This is Base text"
                            android:textSize="18sp" />

                        <TextView
                            android:id="@+id/amount"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_marginTop="2dp"
                            android:layout_weight="1"
                            android:ellipsize="end"
                            android:gravity="top"
                            android:text="200 Childs "
                            android:textColor="@color/pink"
                            android:textSize="14sp" />
                    </LinearLayout>

                </RelativeLayout>

            </RelativeLayout>

文本视图的背景xml形状:

       <?xml version="1.0" encoding="utf-8"?>
            <shape xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="oval">
                <corners android:radius="10dip"/>
                <stroke android:color="#FF0000" android:width="5dip"/>
                <solid android:color="#FF0000"/>
            </shape>