膨胀布局麻烦

时间:2015-09-30 03:37:21

标签: android

我试图动态地将视图添加到相对布局中。但是这个观点并不是我正在使用的代码包含多个......

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>

1 个答案:

答案 0 :(得分:0)

您可以使用ListView或RecyclerView