Android:以编程方式向布局添加按钮 [英] Android: programmatically adding buttons to a layout
问题描述
我正在尝试根据按钮左侧的编辑文本获取一个添加按钮以将另一个按钮添加到布局中.关键是让一个人列出他们房子里的房间,然后当他们在每个房间里输入时,会生成一个新按钮,这样他们就可以点击房间,然后开始下一页的工作.
I'm trying to get an add button to add another button to the layout, based on the edittext to the left of the button. The point is for a person to list the rooms in their house, and then when they type in each room, a new button is generated so they can click the room, and then start working on the next page.
我已经完成了一个 xml 布局,然后我意识到我正在以编程方式"添加按钮,所以我以编程方式重新布局,然后在开关/案例中(这就是我做 onclicks 的方式)添加按钮我试图在视图中添加一个按钮,但它变得非常棘手.我想在编辑文本下方有一个滚动视图并添加按钮,当他们将所有房间添加到他们的房子时,它最终会填充他们整个家的可滚动按钮列表.有没有办法以编程方式将按钮添加到 xml 布局.我以为你可以,但我正在尝试的一切都不起作用.
I had an xml layout all done, and then I realized I'm "programmatically" adding buttons, so I redid the layout programmatically, and then in the switch/case (that's how I do onclicks) for the add button I tried to add a button to the view, but it's getting very tricky. I'd like to have a scrollview below the edittext and add buttons, and as they add all the rooms to their house it eventually is populated with a scrollable list of buttons for their entire home. Is there a way to add buttons programmatically to an xml'd layout. I was thinking you can but everything I'm trying just isn't working.
感谢大家的帮助,如果您有任何建议,我们将不胜感激.
Thanks for your help everybody, any recommendations you have would be greatly appreciated.
第一次编辑(响应 Tanuj 的解决方案)
First Edit (in response to Tanuj's solution)
我的 XML 文件(不确定我们是要使用这个还是只使用 java):
My XML file (not sure if we're going to use this or just use the java):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvAddARoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvAddARoom" />
<EditText
android:id="@+id/etAddARoom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/etAddARoom" />
<Button
android:id="@+id/btnAddARoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnAdd" />
<TextView
android:id="@+id/tvSelectARoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvSelectARoom" />
<TextView
android:id="@+id/tvNoRooms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvNoRooms" />
<Button
android:id="@+id/btnViewAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnViewAll" />
</LinearLayout>
还有Java.这一点都不正确,因为在 java 中我创建了整个布局,而不是使用上面的布局.只是不确定我是否可以将两者联系起来.
And the Java. This isn't at all correct, as in the java I'm creating the whole layout instead of using the layout above. Just not sure if I can bridge the two.
package com.bluej.movingbuddy;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
//import android.widget.ScrollView;
import android.widget.TextView;
public class EstimatorByRoom extends Activity implements OnClickListener {
String roomName;
EditText etAddARoom;
LinearLayout layout;
LinearLayout.LayoutParams layoutParam;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.estimatorbyroom);
LayoutParams params =
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
//create a layout
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
//create a text view
TextView tvAddARoom = new TextView(this);
tvAddARoom.setText("Add a Room");
tvAddARoom.setLayoutParams(params);
//create an edittext
EditText etAddARoom = new EditText(this);
etAddARoom.setHint("Living Room, Dining Room, etc.");
etAddARoom.setLayoutParams(params);
//create a button
Button btnAddARoom = new Button(this);
btnAddARoom.setText("Add");
btnAddARoom.setLayoutParams(params);
//adds the textview
layout.addView(tvAddARoom);
//add the edittext
layout.addView(etAddARoom);
//add the button
layout.addView(btnAddARoom);
//create the layout param for the layout
LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
this.addContentView(layout, layoutParam);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnAddARoom:
//add a room
//this part isn't working!
roomName = etAddARoom.getText().toString();
Button createdButton = new Button(this);
createdButton.setText(roomName);
layout.addView(createdButton);
this.addContentView(layout, layoutParam);
//if no rooms make tvnorooms disappear
break;
}
}
}
推荐答案
试试这个:
//the layout on which you are working
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
//set the properties for button
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText("Button");
btnTag.setId(some_random_id);
//add button to the layout
layout.addView(btnTag);
这篇关于Android:以编程方式向布局添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!