2014-10-06 86 views
1

我已經瀏覽了一下互聯網瞭解了一下,試圖找到一些關於XML設計的指南。Android正確的佈局設計

到目前爲止,我發現最好的設計是保持佈局「平坦」,這意味着保持佈局嵌套到最小。

基本上我有四個下面的佈局堆疊在一起。我沒有添加超過4個的計劃。每個之間唯一不同的是simple_detail_image中的圖像。我找到的選項是使用<include>爲每個項目,然後以編程方式更改圖像。

這是最佳實踐還是有更實際的東西?

我探索過的另一個選擇是製作一個ListView並用它們填充它,但看起來像是過度殺傷。

<?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> 
+0

如果整個事情正在重複使用'ListView' – kId 2014-10-06 05:41:16

+0

等等,這4個佈局在哪裏?我看到一個4意見。因此這裏的嵌套是零。 – Rob 2014-10-06 06:14:03

+0

我沒有發佈代表它的圖片的聲望。我上面展示的視圖是我想要堆疊的4個視圖。當我提到嵌套時,我指的是儘可能少地使用'LinearLayouts'或它們的等價物。我的文章的要點是要找到**適當**的方式來安排上述代碼到4行。 – Sherlock 2014-10-06 06:37:39

回答

1

您可以使用單個TextView代替整個佈局來簡化整個事物,不需要圖像和容器。

由於TextView可以同時包含不同的複合繪圖。
例如:

android:drawablePadding="4dp" 
android:drawableLeft="@drawable/ic_launcher" 
android:drawableRight="@drawable/ic_chevron_gray" 

您可以在您的外(垂直)容器中的4個TextViews和你做。

然後,在代碼中,改變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); 

這將讓你的佈局超平,高效。