2010-02-07 52 views
5

我有一個基於XML的視圖。創建動態視圖,多視圖X次,每個組的獲取/設置值

http://img513.imageshack.us/img513/7171/contextnotionlevelprefs.jpg http://img513.imageshack.us/img513/7171/contextnotionlevelprefs.jpg

<RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content"><TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Family" android:id="@+id/Family" android:textSize="16px" android:padding="5px" android:textStyle="bold" android:gravity="center_horizontal"></TextView> 
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/Family" android:layout_alignRight="@+id/Family" android:text="@string/context_notion_level_off" android:id="@+id/ContextNotionLevel"></TextView> 
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/Detailed" android:layout_below="@+id/Family" android:layout_alignLeft="@+id/Family" android:textStyle="bold" android:id="@+id/Module" android:text="Location"></TextView> 
<SeekBar android:layout_height="wrap_content" android:id="@+id/SeekBar01" android:layout_width="fill_parent" android:padding="5px" android:saveEnabled="true" android:layout_below="@+id/Module" android:max="2"></SeekBar> 

</RelativeLayout> 

我怎麼能重複下第十節倍,看起來像在這裏:
http://img687.imageshack.us/img687/4674/contextnotionleveldraft.png http://img687.imageshack.us/img687/4674/contextnotionleveldraft.png

然後,我怎麼能訪問搜索條的所有項目?
請注意,我希望這是動態的工作,例如:15次。

如何爲每個組提供名稱(位置),設定值(seekbar值)和檢索值。

回答

10

從你的照片,你不想重複「家庭」稱號

你可以做的是創建一個包含佈局和標題一個新的XML(mymain.xml)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:orientation="vertical" android:id="@+id/myMainLayout" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content"> 
<TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Family" android:id="@+id/Family" android:textSize="16px" android:padding="5px" android:textStyle="bold" android:gravity="center_horizontal"></TextView> 
</LinearLayout> 

然後在另一個XML(myviews.xml),把您從OP把XML(標題爲刪除)

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:id="@+id/RelativeLayout01" 
    android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content"> 

    <TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:textStyle="bold" 
     android:id="@+id/Module" android:text="Location"> 
    </TextView> 

    <TextView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:text="context_notion_level_off" 
     android:layout_alignRight="@+id/Module" android:id="@+id/ContextNotionLevel"> 
    </TextView> 


    <SeekBar android:layout_height="wrap_content" android:id="@+id/SeekBar01" 
     android:layout_width="fill_parent" android:padding="5px" 
     android:saveEnabled="true" android:layout_below="@+id/Module" 
     android:max="2"> 
    </SeekBar> 

</RelativeLayout> 

,並且活動:

super.onCreate(savedInstanceState); 

setContentView(R.layout.mymain); 
LinearLayout l = (LinearLayout) findViewById(R.id.myMainLayout); 
LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

for(int i = 0 ; i < 15; i++) { 
    View myView = linflater.inflate(R.layout.myviews, null); 
    SeekBar s = (SeekBar) myView.findViewById(R.id.SeekBar01); 
    //Set an id to know which seekbar is in use 
    s.setId(i); 
    TextView t = (TextView) myView.findViewById(R.id.Module); 

    //Change name dynamically 
    t.setText(("My location " + i).toString()); 
    s.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){ 

    @Override 
    public void onProgressChanged(SeekBar seekBar, int progress, 
      boolean fromUser) { 
     Log.i("=========","My SeekBar = " + seekBar.getId()); 
     Log.i("=========","My Progress = " + progress); 

    } 

    @Override 
    public void onStartTrackingTouch(SeekBar seekBar) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onStopTrackingTouch(SeekBar seekBar) { 
     // TODO Auto-generated method stub 

    }}); 
    l.addView(myView); 
} 

編輯:

要從mymain.xml添加滾動條,滾動型添加標籤周圍的LinearLayout

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:orientation="vertical" android:scrollbars="vertical"> 
<LinearLayout android:orientation="vertical" android:id="@+id/myMainLayout" android:layout_width="fill_parent" android:layout_height="wrap_content"> 
<TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Family" android:id="@+id/Family" android:textSize="16px" android:padding="5px" android:textStyle="bold" android:gravity="center_horizontal"></TextView> 
</LinearLayout> 

</ScrollView> 
+0

哪裏/我如何添加一個滾動型時有太多的展示上屏幕? – Pentium10 2010-02-07 22:13:17

+0

你把ScrollView放在mymain.xml的LinearLayout周圍 – ccheneson 2010-02-07 22:18:35

+0

呵呵,我錯過了xmlns:android for scrollview(謝謝) – Pentium10 2010-02-07 22:32:19