How to add a button control to an android xml view

2019-01-20 08:53发布

I have an android xml layout, main.xml. I would like to add controls to this layout at runtime (I would like to add a series of additional linear layouts that contain buttons controls). Can I do that and if yes, how?

Thanks

6条回答
Lonely孤独者°
2楼-- · 2019-01-20 09:38

Ok, I have got it to work.

The steps are the following: First inflate the xml layout, ie,

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

Then instantiate the container object from the xml layout into a ViewGroup class, ie,

ViewGroup container = (ViewGroup) view.findViewById(R.id.myContainer);

Then create a linearLayout object, create and add onto that any controls needed, add the linearLayout to the container object and use setContentView on the view object, ie,

container.addView(buttonsLayout);
this.setContentView(view);
查看更多
淡お忘
3楼-- · 2019-01-20 09:39

You can do this quite easy by setting an id on the layout on which you want to add views to. Say your main.xml look like this:

<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>

Lets assume that you want to add your additional views to the LinearLayout with id id/container. In your onCreate method you could retrieve that object for later use:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContainer = (ViewGroup)view.findViewById(R.id.container);
}

Now you are all set to add other views to your container ViewGroup:

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

In the getButtons method you need to create your LinearLayout containing the buttons you need. Either you do this programmatically or by inflating a view defined in an XML file. See LayoutInflater.inflate.

查看更多
三岁会撩人
4楼-- · 2019-01-20 09:42

just try this:

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

now create button dynamically like this

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

now if you want to add onether linearlayout then add it below button then

 LinearLayout llinner = new LinearLayout(this);

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

llinner.addView(btn2 );

mainLinearLayout .addView(llinner);
查看更多
Animai°情兽
5楼-- · 2019-01-20 09:48

I see the error u r doing here

LinearLayout mainLayout = (LinearLayout) findViewById(R.layout.main);

You r taking the layout as Linearlayout object, you should take the LinearLayout id

Try this

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

Button b1 = new Button(this);

b1.setText("Btn");

lnr.addView(b1);
查看更多
Anthone
6楼-- · 2019-01-20 09:49

You can add controls programmatically if you want in your code, or even another XML with a View and an Inflater.

Here you can read the basics: http://developer.android.com/guide/topics/ui/declaring-layout.html

查看更多
老娘就宠你
7楼-- · 2019-01-20 09:52

Try this :

LinearLayout ll =(LinearLayout)findViewById(R.id.linlay);
Button b = new Button(this);
b.setText("Hello");
l.addView(b);

This might help you

查看更多
登录 后发表回答