2012-03-28 3 views
1

Ich möchte ein Array von LinearLayout s erstellen. In meiner Anwendung habe ich eine XML-Datei mit zehn LinearLayout s von diesen LinearLayout s Layout-ID für jedes Layout enthält. Ich würde gerne ein LinearLayout s Array für alle LinearLayouts pflegen. Wie kann ich das machen?Wie erstellt man ein Array für alle LinearLayouts aus main.xml?

+0

y hast du gewählt? –

Antwort

2

Sie können Ihre Layout-IDs in einem int-Array speichern. Beispiel int []idArray;

Wenn Sie diese verwenden möchten, rufen Sie nur findViewById(idArray[index]) an und denken Sie daran, es in ein lineares Layout umzuwandeln.

oder setContentView(idArray[index]).

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); 
    } 
}