2015-09-30 94 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