我有一个名为main.xml的Android XML布局。我想在运行时向此布局添加控件(我想添加一系列包含按钮控件的线性布局)。我可以这样做吗?如果可以,该怎么做?
谢谢。
谢谢。
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);
View view = View.inflate(this, R.layout.main, null);
ViewGroup container = (ViewGroup) view.findViewById(R.id.myContainer);
container.addView(buttonsLayout);
this.setContentView(view);
您可以通过在要添加视图的布局上设置一个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/container
的LinearLayout
中。在您的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
。试试这个:
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);
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
LinearLayout ll =(LinearLayout)findViewById(R.id.linlay);
Button b = new Button(this);
b.setText("Hello");
l.addView(b);