gpt4 book ai didi

android - 如何在另一个 View 下添加动态按钮?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:27:53 25 4
gpt4 key购买 nike

在android中,我有一段代码:

// RelativeLayout with id is "root": main.xml
<EditText
android:id="@+id/pref_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:hint="Text to share in preference"
/>
// This is the button I want to add to main.xml
<Button
android:id="@+id/save_button"
android:layout_below="@id/pref_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
/>

在我的 Activity 中,使用 RelativeLayout.LayoutParam 我可以在 left, right, top, bottom 位置添加 button >root View ,但我不能添加 below 或等等...另一个 View !!那么,任何人都可以建议在 RelativeLayout 中动态添加一个与另一个 view 相关的 view 吗?

最佳答案

给你。这将完成您想要做的事情:

public class ExampleActivity extends Activity {
private RelativeLayout rl;
private EditText editText;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);

rl = (RelativeLayout) findViewById(R.id.main_rl);
editText = (EditText) findViewById(R.id.pref_edit_text);

Button button = new Button(this);
button.setText("Save");

// create the layout params that will be used to define how your
// button will be displayed
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

// add the rule that places your button below your EditText object
params.addRule(RelativeLayout.BELOW, editText.getId());

// set the layoutParams on the button
button.setLayoutParams(params);

// add button to your RelativeLayout
rl.addView(button);
}
}

关于android - 如何在另一个 View 下添加动态按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11480342/

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