gpt4 book ai didi

Android动态RelativeLayout

转载 作者:行者123 更新时间:2023-11-29 14:54:16 26 4
gpt4 key购买 nike

我的 layout.xml 文件如下所示:

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="270dp"
android:layout_weight="0.47" >

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

</ScrollView>

我使用以下代码在 for 循环中创建 TextViewButton:

  View linearLayout =  findViewById(R.id.linearLayout2);


for (int i=0 ; i<10; i++){
TextView textview = new TextView(this);
textview.setText("Text view" + i);
textview.setId(i);

textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
((LinearLayout) linearLayout).addView(textview);


Button button = new Button(this);
button.setText("View");
button.setId(i);
int width=90;
int height=60;
button.setLayoutParams(new LayoutParams(width, height));
((LinearLayout) linearLayout).addView(button);

}

我在 TextView 下方获取了 Button。任何人都可以帮我用 RelativeLayout 替换这个 LinearLayout 这样我就可以并排获得 TextViewButton

最佳答案

修改您的布局,使您拥有 RelativeLayout 而不是 LinearLayout:

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="270dp"
android:layout_weight="0.47" >

<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</RelativeLayout>

</ScrollView>

然后尝试使 TextViewButton 位于同一行:

    RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout);
for (int i = 0; i < 10; i++) {
Button button = new Button(this);
button.setText("View" + i);
button.setId(1000 + i);
int width = 90;
int height = 60;

TextView textview = new TextView(this);
textview.setText("Text view" + i);
textview.setId(2000 + i);

if (i == 0) {
RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
textview.setLayoutParams(rlp2);
rl.addView(textview);
RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
width, height);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button.setLayoutParams(rlp1);
rl.addView(button);
} else {
RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rlp2.addRule(RelativeLayout.BELOW, button.getId() - 1);
textview.setLayoutParams(rlp2);
rl.addView(textview);
RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
width, height);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlp1.addRule(RelativeLayout.BELOW, button.getId() - 1);
button.setLayoutParams(rlp1);
rl.addView(button);
}
}

关于Android动态RelativeLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10110953/

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