Android 如何避免嵌套的LinearLayout或RelativeLayout

Android 如何避免嵌套的LinearLayout或RelativeLayout,android,android-layout,Android,Android Layout,我正在尝试在recyclerview的cardview中创建类似列表的内容 它是一个详细信息框,应该在回收器适配器的getholderview函数中以编程方式填充 现在我想到了下面的代码,但不知怎么的,我觉得这不是很好 如何避免大量嵌套布局来实现这一点?在这种情况下,我是否应该尝试避免这种情况 根线性布局是不必要的,请使用CardView作为根元素 如果没有触摸事件或样式,第二个线性布局也是不必要的。将其替换为子元素下的layout_attr 您可以将包含两个TextView的最里面的Line

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

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

现在我想到了下面的代码,但不知怎么的,我觉得这不是很好

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



根线性布局是不必要的,请使用CardView作为根元素

如果没有触摸事件或样式,第二个线性布局也是不必要的。将其替换为子元素下的
layout_
attr

您可以将包含两个TextView的最里面的LinearLayout放入另一个xml文件中,并将它们包含在
标记中,这可以使您的代码更加简单和干净。并且易于重复使用。像这样

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" />

根线性布局是不必要的,请使用CardView作为根元素

如果没有触摸事件或样式,第二个线性布局也是不必要的。将其替换为子元素下的
layout_
attr

您可以将包含两个TextView的最里面的LinearLayout放入另一个xml文件中,并将它们包含在
标记中,这可以使您的代码更加简单和干净。并且易于重复使用。像这样

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" />


> P>请考虑使用。它的创建旨在为任何复杂的建筑布局提供灵活性。它减少了嵌套布局的数量 谢谢,我试图删除后两个,但是视图不再定位,请查看编辑后的问题。我不明白?你是否在第二个线性布局的子视图中设置了
android:below
?你是说android:layout\u below,studio在autocomplete中没有给我这个选项,我应该设置什么id,cardview的id?删除第二个
LinearLayout
,然后在第三个
LinearLayout
中设置它,这行得通吗,但是我需要对其他代码进行分离,但我喜欢现在的样子谢谢alotthanks,我试图删除后两个,但是视图不再定位,请查看编辑后的问题。我不明白?你是否在第二个线性布局的子视图中设置了
android:below
?你是说android:layout\u below,studio在autocomplete中没有给我这个选项,我应该设置什么id,cardview的id?删除第二个
LinearLayout
,然后在第三个
LinearLayout
中设置它,这行得通吗,但我需要其他代码的分离,但我喜欢它,就像现在一样,非常感谢