2013-10-14 16 views
0

我已經寫了一個應用程序,現在想要設計UI。問題是我擁有相同的數據(好吧,不一樣,但是因爲它是一個XML讀數,所以是同一種數據),現在我想要設計它。但我不認爲我可以通過編程獲得設計。所以我認爲我使用XML來設計一次,然後在for循環中爲其他數據複製佈局。這可能嗎?多次使用一個listview的XML設計

如果是,那怎麼可能?我現在完成了一個條目的XML,現在我希望所有其他條目都具有與其他條目相同的樣式和佈局......謝謝!

+0

你讀過如何使用一個ListView與自定義單元格?點10 http://www.vogella.com/articles/AndroidListView/article.html –

+0

聽起來很有趣。我會試一試。謝謝。將報告回來,但我有很多數據,所以需要一些時間。已經感覺到了! – Musterknabe

回答

0

有一個簡單的方法來實現這一點。

在xml文件中包含標籤,您可以在多個屏幕設計中使用一種佈局。

例如。你爲應用程序創建了頭文件,因此不需要在所有文件中使用相同的代碼,只需在每個xml文件中包含該部分即可。

這裏是一個簡單的例子,可以幫助你。

創建一個可重用的佈局

titlebar.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width=」match_parent」 
    android:layout_height="wrap_content" 
    android:background="@color/titlebar_bg"> 

    <ImageView android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/gafricalogo" /> 
</FrameLayout> 

使用標籤:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width=」match_parent」 
    android:layout_height=」match_parent」 
    android:background="@color/app_bg" 
    android:gravity="center_horizontal"> 

    <include layout="@layout/titlebar"/> 

    <TextView android:layout_width=」match_parent」 
       android:layout_height="wrap_content" 
       android:text="@string/hello" 
       android:padding="10dp" /> 

    ... 

</LinearLayout> 
+0

嘿謝謝,但我想你誤解了我的問題。我不想在我的新佈局中使用其他佈局。我只想設計一次TextView,然後以編程方式將其用於下一個48。我的回答下面的評論幫了我一些。我會嘗試一下。 – Musterknabe

+0

okey然後你可以創建自定義的類,這是通過textview或函數來擴展的,用它的樣式創建文本視圖。 – Hasmukh