我想创建一个LinearLayout
的数组。在我的应用程序中,我设计了一个xml文件,其中LinearLayout
包含10个LinearLayout
个包含每个布局的布局ID。我想为所有LinearLayout
维护一个LinearLayouts
s数组。我怎么能这样做?
答案 0 :(得分:2)
您可以将布局ID存储在int数组中。示例int []idArray;
如果您想使用它们,请只调用findViewById(idArray[index])
并记住将其转换为线性布局。
或setContentView(idArray[index])
。
答案 1 :(得分:0)
//main.xml
<?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="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linear1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/ic_launcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/linear2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/ic_launcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/linear3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/ic_launcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/linear4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/ic_launcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/linear5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/ic_launcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/linear6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/ic_launcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/linear7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/ic_launcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/linear8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/ic_launcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/linear9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/ic_launcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/linear10"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/ic_launcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
//LayoutsTestActivity.java
public class LayoutsTestActivity extends Activity {
ArrayList<LinearLayout>linList;
LinearLayout lin1,lin2,lin3,lin4,lin5,lin6,lin7,lin8,lin9,lin10;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
linList=new ArrayList<LinearLayout>();
lin1=(LinearLayout)findViewById(R.id.linear1);
lin2=(LinearLayout)findViewById(R.id.linear2);
lin3=(LinearLayout)findViewById(R.id.linear3);
lin4=(LinearLayout)findViewById(R.id.linear4);
lin5=(LinearLayout)findViewById(R.id.linear5);
lin6=(LinearLayout)findViewById(R.id.linear6);
lin7=(LinearLayout)findViewById(R.id.linear7);
lin8=(LinearLayout)findViewById(R.id.linear8);
lin9=(LinearLayout)findViewById(R.id.linear9);
lin10=(LinearLayout)findViewById(R.id.linear10);
linList.add(lin1);
linList.add(lin2);
linList.add(lin3);
linList.add(lin4);
linList.add(lin5);
linList.add(lin6);
linList.add(lin7);
linList.add(lin8);
linList.add(lin9);
linList.add(lin10);
}
}