2015-09-30 125 views
0

我試圖動態地添加一個視圖到相對佈局。但鑑於沒有multyling我使用的是封閉的代碼...充氣佈局麻煩

for (int i=0;i<mThumbIds.length-1;i++) { 

    try { 

     RelativeLayout containerLinearLayout = (RelativeLayout)findViewById(R.id.maincontainer); 
     View child = getLayoutInflater().inflate(R.layout.single_row, null); 
     containerLinearLayout.addView(child); 

    }catch (Exception e){ 
     Toast.makeText(getApplicationContext(),"dkljf"+e.getMessage(),Toast.LENGTH_SHORT).show(); 
    } 

    } 

Activitymain.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
tools:context=".MainActivity" 
android:orientation="vertical"> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 


<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/scrollView" > 
    <RelativeLayout android:id="@+id/maincontainer" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal"> 



    </RelativeLayout> 
    </ScrollView> 

single_row.xml

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

    <LinearLayout android:id="@+id/oddcontainer" 
     android:layout_width="fill_parent" 
     android:layout_height="150dp" 
     android:orientation="horizontal" 
     android:layout_weight="1" 
     android:background="#f00"> 

    </LinearLayout> 

    <LinearLayout android:id="@+id/evencontainer" 
     android:layout_width="fill_parent" 
     android:layout_height="150dp" 
     android:orientation="horizontal" 
     android:layout_weight="1" 
     android:background="#f0f"> 

    </LinearLayout> 


</LinearLayout> 

+0

你可以嘗試將容器佈局移到for循環之外嗎? –

+0

我也試過..... – Geethu

+0

是你輸入循環的代碼,你會得到什麼異常? –

回答

0

您可以使用列表查看或RecyclerView

+0

其實我不能使用列表視圖 – Geethu