2013-10-03 34 views
0

我正在開發中的Android應用程序。應用程序應該列出項目。它看起來如下有點:sampleXML VS做編程

的問題是我需要列出約40項以上的方式。我可以使用xml文件中的相對佈局來做到這一點。但它太長了。例如,對於只創造3項我的代碼看起來像巨大的下面(更不用說40):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/tvb" > 

     <ImageView 
      android:id="@+id/image_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="3dp" 
      android:adjustViewBounds="true" 
      android:maxHeight="60dp" 
      android:maxWidth="100dp" 
      android:scaleType="fitCenter" 
      android:src="@drawable/tomato" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/image_view" 
      android:text="Tomato" 
      android:textColor="#000000" 
      android:textSize="25dp" 
      android:textStyle="bold" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:text="20gm" 
      android:textColor="#000000" 
      android:textSize="20dp" 
      android:textStyle="bold" /> 
    </RelativeLayout> 
    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/tvb" > 

     <ImageView 
      android:id="@+id/image_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="3dp" 
      android:adjustViewBounds="true" 
      android:maxHeight="60dp" 
      android:maxWidth="100dp" 
      android:scaleType="fitCenter" 
      android:src="@drawable/begun" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/image_view" 
      android:text="Begun" 
      android:textColor="#000000" 
      android:textSize="25dp" 
      android:textStyle="bold" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:text="45gm" 
      android:textColor="#000000" 
      android:textSize="20dp" 
      android:textStyle="bold" /> 
    </RelativeLayout> 
    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/tvb" > 

     <ImageView 
      android:id="@+id/image_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="3dp" 
      android:adjustViewBounds="true" 
      android:maxHeight="60dp" 
      android:maxWidth="100dp" 
      android:scaleType="fitCenter" 
      android:src="@drawable/potol" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/image_view" 
      android:text="Potol" 
      android:textColor="#000000" 
      android:textSize="25dp" 
      android:textStyle="bold" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:text="35gm" 
      android:textColor="#000000" 
      android:textSize="20dp" 
      android:textStyle="bold" /> 
    </RelativeLayout> 

</LinearLayout> 

不過,我可以複製和粘貼,並休息37項做到這一點。但它真的很有效率嗎?這意味着得到xml文件太長了......是否真的有效...... 再次在java中執行它可能會減慢代碼。因爲我需要爲每個列表項添加圖像。

我期待一個良好的指導。任何人都可以建議

+0

使用一個ListView你可以重用你的項目的看法。 – nhaarman

回答

0

您可以創建一個行獨立佈局的XML,然後將其包含在主佈局多次,你need.-

_row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/tvb" > 

    <ImageView 
     android:id="@+id/image_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="3dp" 
     android:adjustViewBounds="true" 
     android:maxHeight="60dp" 
     android:maxWidth="100dp" 
     android:scaleType="fitCenter" 
     android:src="@drawable/tomato" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/image_view" 
     android:text="Tomato" 
     android:textColor="#000000" 
     android:textSize="25dp" 
     android:textStyle="bold" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:text="20gm" 
     android:textColor="#000000" 
     android:textSize="20dp" 
     android:textStyle="bold" /> 
</RelativeLayout> 

main_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <include 
     android:id="@+id/row1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     layout="@layout/_row" /> 

    <include 
     android:id="@+id/row2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     layout="@layout/_row" /> 

    ... 

</LinearLayout> 

注意的動態列表它最好能夠去一個ListView,或者如果您的列表是'靜態',至少您需要使用ScrollView

+0

然後,我是如何從只更改項目名稱包括在列表中的每個項目一樣一行是番茄和番茄的畫面,但對於另一行是不一樣的。請建議 – exponentialFun

+0

那麼,那不會那麼直截了當;您需要使用自定義屬性創建自定義類視圖,如下所述:http://developer.android.com/training/custom-views/create-view.html – ssantos