gpt4 book ai didi

android - 循环内的动态布局充气器

转载 作者:行者123 更新时间:2023-11-29 00:39:50 26 4
gpt4 key购买 nike

我想根据来自 parcelable 数组的数据循环在 Activity 中显示 x 个 xml 布局。对于此示例,我已将数组大小更改为固定值 3。

我要膨胀的布局的 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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioGroup android:id="@+id/rg_type"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes"
android:tag="1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No"
android:tag="0" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Don't Know"
android:tag="3" />
</RadioGroup>
</LinearLayout>

父页面的xml为:

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

这是 Activity 的代码:

public class PainRecord_FollowUp extends Activity {

LinearLayout llContent;
Context context;
PainDataItem pd_in; //this is the Parcelable extra received

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.painrecord_followup);

Bundle extras = getIntent().getExtras();
if (extras != null) {
pd_in = extras.getParcelable("m_PainDataItem");
}
LayoutInflater layoutInflator = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout insertPoint = (LinearLayout) findViewById(R.id.ll_content);
List views = new ArrayList();

for(int i=0; i<3; i++){
View view = layoutInflator.inflate(R.layout.row_painrecord_followup, null);
TextView textView = (TextView) view.findViewById(R.id.tv_title);
textView.setText("did x help " + i);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
views.add(view);
}
for(int i = 0; i<views.size(); i++)
insertPoint.addView((View) views.get(i));
}
}

它不是将每个布局显示在彼此下方,而是像这样显示:

enter image description here

如果我更改为 LayoutParams.FILL_PARENT,则只显示 [0] 循环值。

收到的任何建议。

谢谢

最佳答案

将您的 LinearLayout 方向设置为垂直

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

关于android - 循环内的动态布局充气器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10158443/

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