布局参考

时间:2011-11-27 17:15:09

标签: android layout

我有这个XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/fondoverde3"
    android:gravity="top"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <com.google.ads.AdView
            android:id="@+id/ad"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            ads:adSize="BANNER"
            ads:adUnitId="adasdasdasdas"
            ads:loadAdOnCreate="true" />
    </LinearLayout>

    <ScrollView
        android:id="@+id/ScrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:id="@+id/RelativeLayout1"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            ......

            <LinearLayout
                android:id="@+id/LinearLayout2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/linearLayout4"
                android:layout_marginTop="10dp"
                android:orientation="vertical" >
            </LinearLayout>

        </RelativeLayout>
    </ScrollView>

</LinearLayout>

所以我想在我的java代码中引用LinearLayout2添加一些东西。我用一个简单的代码来证明我的代码是有效的(并且它可以工作!!我展示了两个textview),正是这样:

    LinearLayout ll = (LinearLayout)findViewById(R.id.LinearLayout2);
    ll.setOrientation(LinearLayout.VERTICAL);
    TextView tituloIngr2 = new TextView(this);
tituloIngr2.setText("AAAAA");
ll.addView(tituloIngr2);
    TextView tituloIngr1 = new TextView(this);
tituloIngr1.setText("BBBBB");
ll.addView(tituloIngr1);

但我想要做的是在这个linearLayout2中显示我从StringArray获取的许多Textview,所以代码是下一个:

    LinearLayout ll = (LinearLayout)findViewById(R.id.LinearLayout2);
    ll.setOrientation(LinearLayout.VERTICAL);

    for (int m= 0; m<few.length; m++) {

        TextView ingr= new TextView(this);
        ingr.setText(few[m]);

        ingr.setPadding(5, 5, 5, 5);
        ingr.setGravity(0x11);//sacados todos los values de developers
        ingr.setTextColor(0x96610098);
        ingr.setBackgroundResource(android.R.color.white);
        ingr.setTextSize(22);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(                                           LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(0, 25, 0, 0);
    ll.addView(ingr, layoutParams);
}
    this.setContentView(ll);

错误是一个强制关闭,而log cat:

11-27 17:09:58.213: E/AndroidRuntime(1440): java.lang.RuntimeException: Unable to start activity ComponentInfo{back.now/back.now.Tree}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

1 个答案:

答案 0 :(得分:2)

删除此电话:

this.setContentView(ll);

ll已经是内容视图的一部分。尝试再次添加它作为内容视图可能是触发异常的原因。你也不需要这个电话:

ll.setOrientation(LinearLayout.VERTICAL);

在xml中将方向指定为垂直。