gpt4 book ai didi

android - 如何通过代码在 RelativeLayout 中定位 Button?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:56:08 35 4
gpt4 key购买 nike

< RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
< Button android:text="Previous"
android:layout_height="wrap_content"
android:id="@+id/MeasurePrev"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content">
< / Button>
< / RelativeLayout >

谁能告诉我如何使用 Java 代码或在 Activity 类中执行此操作?我不知道如何设置 android:layout_alignParentBottom="true"。我想通过 java 代码实现整个 View 。

最佳答案

这是您的代码的动态等价物:

    RelativeLayout rl = new RelativeLayout(this);
LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rl.setLayoutParams(params);
Button button = new Button(this);
button.setText("Previous");
LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
button.setLayoutParams(params1);
rl.addView(button);

关于android - 如何通过代码在 RelativeLayout 中定位 Button?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3341311/

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