2012-10-08 15 views
1

是否可以創建一個xml模塊/結構,以便使用自定義設置將其包含在另一個xml中?Android:在xml中創建一個可更改的模塊

這個想法是使一個模塊與一個seekbar,一個文本和一個文本輸入。我將在另一個xml中需要這個模塊大約20次,但是它的值(如文本和id)對於每個實例都是不同的。

有沒有辦法在XML中做到這一點?

或者有沒有辦法讓一個類使用這個基本的XML模塊,並讓我通過XML設置所需的值?

回答

1

是的,您可以在xml佈局中使用合併和包含標籤。例如,在grid_layout_view.xml你有這樣的代碼:

<?xml version="1.0" encoding="utf-8"?><GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 
    <include android:id="@+id/my_inside_grid_layout" 
      layout="@layout/grid_layout_inside"/> </GridLayout> 

注意利用標籤的導入包含以下的grid_layout_inside.xml文件的內容:

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

    <ListView 
     android:background="#FFFFFFFF" 
     android:layout_gravity="fill"> 
    </ListView> 
    <LinearLayout 
     android:layout_gravity="fill_horizontal" 
     android:orientation="horizontal" 
     android:padding="5dp"> 

     <Button 
      android:text="Cancel" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 
     <Button 
      android:text="OK" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 
    </LinearLayout> 

</merge> 

跳這個幫助, Ruben

+0

感謝您的快速回復。例如,是否有可能在grid_layout_view.xml中多次包含grid_layout_inside.xml,並且能夠爲每個爲grid_layout_view.xml中的取消按鈕設置不同的文本? – user1628803

相關問題