如何避免嵌套的LinearLayout或RelativeLayout

时间:2016-07-15 22:06:35

标签: android android-layout

我正在尝试在recyclerview中创建类似于cardview内的列表。

这是一个详细信息框,应该在回收器适配器的getholderview函数中以编程方式填充。

现在我提出了以下代码,但不知怎的,我感觉这不是很有效。

如何避免大量嵌套布局来实现这一点,在这种情况下我是否应该尽量避免使用它?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:card_view="http://schemas.android.com/apk/res-auto"
              xmlns:app="http://schemas.android.com/tools"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">

    <android.support.v7.widget.CardView

        <RelativeLayout ...>
            ... Other content Title
<ImageView
            android:id="@+id/profileImage"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/ic_loyalty_black_24dp"
            />

<TextView
    android:id="@+id/cell_textView"
    android:layout_toRightOf="@id/profileImage"
    android:layout_toEndOf="@id/profileImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Medium Text"
    android:focusable="true"
    android:clickable="true"
    android:foreground="?android:attr/selectableItemBackground"
    android:textAppearance="?android:attr/textAppearanceMedium"/>

<TextView
    android:id="@+id/fat_bar_text"
    android:layout_toRightOf="@id/profileImage"
    android:layout_toEndOf="@id/profileImage"
    android:layout_below="@id/cell_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Fett:"
    android:layout_marginTop="3dp"
    android:textAppearance="?android:attr/textAppearanceSmall"/>

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                          android:id="@+id/detailBox"
                          android:layout_width="fill_parent"
                          android:layout_height="wrap_content"
                          android:orientation="vertical"
                          android:layout_below="@id/fat_bar_text"
                          android:gravity="bottom">

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

                    <TextView
                        android:layout_gravity="start"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.5"
                        android:text="Value:"/>

                    <TextView
                        android:layout_weight="0.5"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="500€"/>
                </LinearLayout>

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

                    <TextView
                        android:layout_gravity="start"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.5"
                        android:text="Test:"/>

                    <TextView
                        android:layout_weight="0.5"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="blah"/>
                </LinearLayout>
            </LinearLayout>
        </RelativeLayout>


    </android.support.v7.widget.CardView>
</LinearLayout>

2 个答案:

答案 0 :(得分:2)

根LinearLayout是不必要的,使用CardView作为根元素。

如果您没有触摸事件或样式,则第二个LinearLayout也是不必要的。将其替换为子元素中的layout_below attr

你可以把包含两个TextView的最里面的LinearLayout放到另一个xml文件中,并用<include>标签包含它们,这可以使你的代码更简单和干净。并且易于重复使用。像这样

mytext.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_gravity="start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="Value:"/>

    <TextView
        android:layout_weight="0.5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="500€"/>
</LinearLayout>

cardview.xml

<include 
    layout="@layout/mytext"
    android:id="@+id/mytext1"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<include 
    layout="@layout/mytext"
    android:id="@+id/mytext2"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

答案 1 :(得分:2)

请考虑使用Constraint Layout。它的创建是为了提供任何复杂性的构建布局的灵活性。它减少了嵌套布局的数量。 One more link