【发布时间】:2020-10-22 22:36:35
【问题描述】:
正如标题所述,我确实想以编程方式将按钮添加到 ConstraintLayout,这意味着, 在 XML 文件中可以看到添加的按钮。
XML 文件:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/idMain">
</androidx.constraintlayout.widget.ConstraintLayout>
添加按钮的代码:
ConstraintLayout cst = (ConstraintLayout) findViewById(R.id.idMain);
Button btn = new Button(this);
btn.setText("Hallo Welt");
cst.addView(btn);
问题:添加的按钮在 XML 文件中无处可见。 问题:如何将按钮附加到 XML 文件?
向上
【问题讨论】:
标签: android xml android-studio button