gpt4 book ai didi

android - 布局引用

转载 作者:行者123 更新时间:2023-11-29 00:42:57 25 4
gpt4 key购买 nike

我有这个 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);

错误是强行关闭,日志猫:

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.

最佳答案

删除此调用:

this.setContentView(ll);

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

ll.setOrientation(LinearLayout.VERTICAL);

方向在 xml 中指定为垂直。

关于android - 布局引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8287368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com