代码改变世界

xml嵌套和循环控件资源

2012-11-05 22:40  bitfairyland  阅读(2634)  评论(0编辑  收藏  举报

 

最近问卷输入项很多,大致分为几个类型,如果正常布局就非常繁琐而且比较乱

用到了xml嵌套

子xml,chose.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" 
 5     android:orientation="vertical">
 6 <LinearLayout 
 7         android:layout_width="match_parent"
 8           android:layout_height="match_parent"
 9           android:orientation="vertical"
10           android:paddingTop="30dip">
11     <LinearLayout 
12         android:layout_width="match_parent"
13           android:layout_height="match_parent"
14           android:orientation="horizontal"
15           android:layout_weight="7"
16           android:paddingLeft="20dip">
17         <TextView 
18                android:layout_width="wrap_content"
19             android:layout_height="wrap_content"
20             android:layout_centerHorizontal="true"
21             android:layout_centerVertical="true"
22             tools:context=".MainActivity"
23             android:id="@+id/question"
24             android:text="Q2:您的文化程度?"
25             android:textSize="12pt"
26             android:textColor="#3b3b3b"
27         />
28         <EditText 
29             android:id="@+id/txt_choice"
30             android:layout_width="50dip"
31             android:layout_height="wrap_content"
32             android:textSize="12pt"
33             android:layout_marginLeft="5dip"
34             android:textColor="#3b3b3b"
35             android:background="@drawable/bgtxts"
36             
37                 />
38     </LinearLayout>
39     <LinearLayout 
40         android:layout_width="match_parent"
41           android:layout_height="match_parent"
42           android:orientation="vertical"
43           android:paddingTop="10dip"
44           android:layout_weight="4"
45           android:paddingLeft="20dip" >
46         
47          <TextView 
48                android:layout_width="wrap_content"
49             android:layout_height="wrap_content"
50             android:layout_centerHorizontal="true"
51             android:layout_centerVertical="true"
52             tools:context=".MainActivity"
53             android:text="1.aaaaa"
54             android:id="@+id/ch_one"
55             android:textSize="12pt"
56             
57             android:textColor="#3b3b3b"/>
58          </LinearLayout>
59 
60     
61 </LinearLayout>
62     
63     
64 
65 </LinearLayout>

singletext.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" 
 5     android:orientation="vertical">
 6 <LinearLayout 
 7         android:layout_width="match_parent"
 8           android:layout_height="match_parent"
 9           android:orientation="vertical"
10           android:paddingTop="30dip">
11     <LinearLayout 
12         android:layout_width="match_parent"
13           android:layout_height="match_parent"
14           android:orientation="horizontal"
15           android:layout_weight="7"
16           android:paddingLeft="20dip">
17         <TextView 
18                android:layout_width="wrap_content"
19             android:layout_height="wrap_content"
20             android:layout_centerHorizontal="true"
21             android:layout_centerVertical="true"
22             tools:context=".MainActivity"
23             android:id="@+id/question"
24             android:text="Q2:您的文化程度?"
25             android:textSize="12pt"
26             android:textColor="#3b3b3b"
27         />
28         <EditText 
29             android:id="@+id/txt_choice"
30             android:layout_width="50dip"
31             android:layout_height="wrap_content"
32             android:textSize="12pt"
33             android:layout_marginLeft="5dip"
34             android:textColor="#3b3b3b"
35             android:background="@drawable/bgtxts"
36             
37                 />
38     </LinearLayout>
39     <LinearLayout 
40         android:layout_width="match_parent"
41           android:layout_height="match_parent"
42           android:orientation="vertical"
43           android:paddingTop="10dip"
44           android:layout_weight="4"
45           android:paddingLeft="20dip" >
46         
47          <TextView 
48                android:layout_width="wrap_content"
49             android:layout_height="wrap_content"
50             android:layout_centerHorizontal="true"
51             android:layout_centerVertical="true"
52             tools:context=".MainActivity"
53             android:text="1.aaaaa"
54             android:id="@+id/ch_one"
55             android:textSize="12pt"
56             
57             android:textColor="#3b3b3b"/>
58          </LinearLayout>
59 
60     
61 </LinearLayout>
62  
63 </LinearLayout>

 

父xml

 1  <LinearLayout 
 2         android:layout_width="match_parent"
 3           android:layout_height="match_parent"
 4           android:orientation="vertical"
 5           android:paddingTop="5dip"
 6           android:layout_weight="1"
 7           android:paddingLeft="2dip">
 8 
 9     <include
10         android:id="@+id/q14"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         layout="@layout/choice" />
14     <include
15         android:id="@+id/q15"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         layout="@layout/choice" />
19     <include
20         android:id="@+id/q16"
21         android:layout_width="wrap_content"
22         android:layout_height="wrap_content"
23         layout="@layout/singletext" />
24     <include
25         android:id="@+id/q17"
26         android:layout_width="wrap_content"
27         android:layout_height="wrap_content"
28         layout="@layout/choice" />
29     <include
30         android:id="@+id/q18"
31         android:layout_width="wrap_content"
32         android:layout_height="wrap_content"
33         layout="@layout/singletext" />
34     <include
35         android:id="@+id/q19"
36         android:layout_width="wrap_content"
37         android:layout_height="wrap_content"
38         layout="@layout/choice" />
39 </LinearLayout>

控件的循环调用,里边涉及两点,一、通过控件名称获得空间句柄,二、从父布局中获得子布局中的xml

 1 for (int i = 14; i < 20; i++) {
 2             question = QCommon.getQuestion(i);
 3             
 4             //通过控件名称调用控件
 5             int resID = getResources().getIdentifier("q" + String.valueOf(i),
 6                     "id", getPackageName());
 7             mLLayout = (LinearLayout) findViewById(resID);
 8 
 9             if (question.getType() == 0) {
10 
11                 //从父控件中获得子xml中的控件
12                 mquestion=(TextView)mLLayout.findViewById(R.id.question);
13                 mquestion.setText("Q"+String.valueOf(i)+question.getTitle());
14             } else {
15 
16                 mquestion=(TextView)mLLayout.findViewById(R.id.question);
17                 
18                 mquestion.setText("Q"+String.valueOf(i)+question.getTitle());
19                 
20                 if(question.getContent().length()>0){
21                     tvone=(TextView)mLLayout.findViewById(R.id.ch_one);
22                     tvone.setText(question.getContent());
23                 }
24             }
25 
26         }