如何在Android XML视图中动态添加按钮控件?

8
我有一个名为main.xml的Android XML布局。我想在运行时向此布局添加控件(我想添加一系列包含按钮控件的线性布局)。我可以这样做吗?如果可以,该怎么做?
谢谢。
7个回答

6
我看到你在这里犯了一个错误。
LinearLayout mainLayout = (LinearLayout) findViewById(R.layout.main);

您正在将布局视为LinearLayout对象,您应该使用LinearLayout的id

尝试这样做

LinearLayout lnr = (LinearLayout) findViewById(R.id.LinearLayout01);

Button b1 = new Button(this);

b1.setText("Btn");

lnr.addView(b1);

是的,但按钮在xml中未定义。我想要做以下事情:Button b = new Button(this); LinearLayout mainLayout = (LinearLayout)findViewById(R.layout.main); mainLayout.addView(b),我尝试这样做,但出现错误。这样做对吗? - a.p.
什么错误?你尝试过 addView(b, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)) 吗? - gngr44

3
好的,我已经让它工作了。
步骤如下: 首先膨胀XML布局,即,

View view = View.inflate(this, R.layout.main, null);

将XML布局文件中的容器对象实例化为ViewGroup类,例如:
ViewGroup container = (ViewGroup) view.findViewById(R.id.myContainer);

然后创建一个LinearLayout对象,创建并添加所需的任何控件,将LinearLayout添加到容器对象中,并在视图对象上使用setContentView。
container.addView(buttonsLayout);
this.setContentView(view);

3

我已经阅读了该文档,但它没有提到如何将在运行时创建的新控件添加到现有的 XML 布局文件中。 - a.p.

0

您可以通过在要添加视图的布局上设置一个id来轻松实现此操作。假设您的main.xml如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  <TextView android:id="@+id/label"
      android:layout_width="fill_parent"/>
  <LinearLayout android:id="@+id/container"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
  </LinearLayout>
</LinearLayout>

假设您想将附加视图添加到ID为id/containerLinearLayout中。在您的onCreate方法中,您可以检索该对象以供以后使用:
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContainer = (ViewGroup)view.findViewById(R.id.container);
}

现在,您已经准备好将其他视图添加到您的容器ViewGroup中了:

LinearLayout theButtons = getButtons()
mContainer.addView(theButtons);

getButtons 方法中,您需要创建包含所需按钮的 LinearLayout。您可以通过编程方式或通过填充在 XML 文件中定义的视图来完成此操作。请参见 LayoutInflater.inflate

0

试试这个:

LinearLayout mainLinearLayout = (LinearLayout) findViewById(R.layout.llmain);

现在可以像这样动态创建按钮

 Button btn1 = new Button(this);
 btn1.setText=("Button 1");
 mainLinearLayout .addView(btn1);

现在如果你想添加另一个LinearLayout,那么就在按钮下面添加它。

 LinearLayout llinner = new LinearLayout(this);

 Button btn2 = new Button(this);
 btn2.setText=("Button 2");
mainLinearLayout .addView(btn2);

llinner.addView(btn2 );

mainLinearLayout .addView(llinner);

0
这是我为了在表格布局上显示一组运行时按钮所做的事情。

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    View view = View.inflate(this, R.layout.activity_main, null);
    TableRow myrow = (TableRow) view.findViewById(R.id.myrow);

    Button btn1 = new Button(this);
    btn1.setText("Button 1");
    myrow .addView(btn1);

    Button btn2 = new Button(this);
    btn2.setText("Button 2");
    myrow .addView(btn2);
    this.setContentView(view);
}
}

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
     <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">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ScrollView
    android:id="@+id/myview"
    android:layout_width="404dp"
    android:layout_height="691dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TableLayout
        android:id="@+id/mylayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TableRow android:id="@+id/myrow"></TableRow>
    </TableLayout>

</ScrollView>

   </androidx.constraintlayout.widget.ConstraintLayout>

AVD Pixel4API30

输出截图 代码输出结果


0
尝试这个:
LinearLayout ll =(LinearLayout)findViewById(R.id.linlay);
Button b = new Button(this);
b.setText("Hello");
l.addView(b);

这可能会对你有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接