我正在開發中的Android應用程序。應用程序應該列出項目。它看起來如下有點:XML 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中執行它可能會減慢代碼。因爲我需要爲每個列表項添加圖像。
我期待一個良好的指導。任何人都可以建議
使用一個ListView你可以重用你的項目的看法。 – nhaarman