Android 正确的布局设计

Android 正确的布局设计,android,android-layout,Android,Android Layout,我在互联网上查阅了大约一段时间,试图找到一些关于XML设计的指南 到目前为止,我发现最好的设计是保持布局“平坦”,这意味着保持布局嵌套最小 基本上我有四个下面的布局堆叠在一起。我没有计划增加4个以上。每种图像之间唯一不同的是简单的“细节”图像中的图像。我发现的选项是对每个项目使用,然后以编程方式更改图像 这是最佳做法还是有更实际的做法 我探索过的另一个选择是制作一个列表视图,并用这些视图填充它,但这似乎有些过分 <?xml version="1.0" encoding="utf-8"?&g

我在互联网上查阅了大约一段时间,试图找到一些关于XML设计的指南

到目前为止,我发现最好的设计是保持布局“平坦”,这意味着保持布局嵌套最小

基本上我有四个下面的布局堆叠在一起。我没有计划增加4个以上。每种图像之间唯一不同的是简单的“细节”图像中的图像。我发现的选项是对每个项目使用
,然后以编程方式更改图像

这是最佳做法还是有更实际的做法

我探索过的另一个选择是制作一个
列表视图
,并用这些视图填充它,但这似乎有些过分

<?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="wrap_content"
              android:background="@drawable/shape_rounded"
              android:padding="4dp"
              android:orientation="horizontal">

    <ImageView
        android:id="@+id/simple_detail_image"
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:src="@drawable/ic_launcher"/>

    <TextView
        android:id="@+id/simple_detail_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="12dp"
        android:layout_marginStart="12dp"
        android:text="Hi"/>


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

    <ImageView
        android:layout_gravity="center"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginRight="@dimen/chevron_horizontal_margin"
        android:layout_marginEnd="@dimen/chevron_horizontal_margin"
        android:src="@drawable/ic_chevron_gray"/>
</LinearLayout>

您可以使用一个文本视图来代替整个布局、无图像和无容器,从而简化整个过程

因为TextView可以同时包含不同的复合可绘图项。
例如:

android:drawablePadding="4dp"
android:drawableLeft="@drawable/ic_launcher"
android:drawableRight="@drawable/ic_chevron_gray"
您可以在外部(垂直)容器中包含4个文本视图,就完成了

然后,在代码中,要更改drawableLeft:

//public void setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
public void setCompoundDrawablesWithIntrinsicBounds (R.drawable.your_left_drawable_1, 0, R.drawable.ic_chevron_gray, 0);

这将使您的布局保持超级平坦和高效。

使用
ListView
如果整个过程都在重复等待,那么这4个布局在哪里?我看到一个有4个视图。因此,这里的筑巢是零。我没有名声张贴一张代表它的图片。我上面显示的视图是我想要堆叠的4个视图。当我提到嵌套时,我指的是使用尽可能少的“线性布局”或其等价物。我这篇文章的重点是找到将上述代码排列成4行的正确方法。