安卓:编程按钮添加到布局 [英] Android: programmatically adding buttons to a layout

查看:157
本文介绍了安卓:编程按钮添加到布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到一个Add按钮添加另一个按钮布局的基础上,为的EditText按钮的左侧。问题的关键是一个人列出的房间在他们家,然后,当他们在每个房间类型,会生成一个新的按钮,使他们可以单击该房间,然后开始工作的下一个页面上。

我有一个XML布局全部完成,然后我意识到我是编程添加按钮,所以我编程重做的布局,然后在开关/箱(这就是我该怎么办onclicks)的添加按钮我尝试添加一个按钮的观点,但它变得非常棘手。我想有以下的EditText一个滚动视图,并添加按钮,并为他们添加所有的客房以他们的房子它最终会填充按钮为他们整个家庭的滚动列表。有没有一种方法以编程方式添加按钮,在xml'd布局。我想你可以,但一切我想只是不工作。

感谢您的帮助大家,任何建议,你会大大AP preciated。

首先编辑(响应Tanuj的解决方案)

我的XML文件(不知道,如果我们要使用这个或只使用java):

 < XML版本=1.0编码=UTF-8&GT?;
 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
     机器人:layout_width =match_parent
     机器人:layout_height =match_parent
     机器人:方向=垂直>

<的TextView
    机器人:ID =@ + ID / tvAddARoom
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文本=@字符串/ tvAddARoom/>

<的EditText
    机器人:ID =@ + ID / etAddARoom
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:提示=@字符串/ etAddARoom/>


<按钮
    机器人:ID =@ + ID / btnAddARoom
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文本=@字符串/ btnAdd/>

<的TextView
    机器人:ID =@ + ID / tvSelectARoom
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文本=@字符串/ tvSelectARoom/>

<的TextView
    机器人:ID =@ + ID / tvNoRooms
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文本=@字符串/ tvNoRooms/>

<按钮
    机器人:ID =@ + ID / btnViewAll
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文本=@字符串/ btnViewAll/>

 < / LinearLayout中>
 

和Java。这不是在所有正确的,因为在java我创建整个布局,而不是使用上述的布局。只是不知道如果我能弥合两种。

 包com.bluej.movi​​ngbuddy;

进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.ViewGroup.LayoutParams;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.LinearLayout;
//进口android.widget.ScrollView;
进口android.widget.TextView;

公共类EstimatorByRoom扩展活动实现OnClickListener {
字符串ROOMNAME;
的EditText etAddARoom;
的LinearLayout布局;
LinearLayout.LayoutParams layoutParam;


@覆盖
保护无效的onCreate(包savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.estimatorbyroom);

    的LayoutParams PARAMS =
            新LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);

    //创建一个布局
    的LinearLayout布局=新的LinearLayout(本);
    layout.setOrientation(LinearLayout.VERTICAL);

    //创建一个文本视图
    TextView的tvAddARoom =新的TextView(本);
    tvAddARoom.setText(添加一个房间);
    tvAddARoom.setLayoutParams(PARAMS);

    //创建一个EditText
    EditText上etAddARoom =新的EditText(本);
    etAddARoom.setHint(客厅,餐厅等。);
    etAddARoom.setLayoutParams(PARAMS);


    //创建一个按钮
    按钮btnAddARoom =新的按钮(这一点);
    btnAddARoom.setText(添加);
    btnAddARoom.setLayoutParams(PARAMS);

    //添加的TextView
    layout.addView(tvAddARoom);

    //添加的EditText
    layout.addView(etAddARoom);
    //添加按钮
    layout.addView(btnAddARoom);

    //创建布局参数的布局
    LinearLayout.LayoutParams layoutParam =新LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT);

    this.addContentView(布局,layoutParam);
}

公共无效的onClick(视图v){
    // TODO自动生成方法存根
    开关(v.getId()){

    案例R.id.btnAddARoom:
        //添加一个房间

        //这部分是不工作!
        。ROOMNAME = etAddARoom.getText()的toString();
        按钮createdButton =新的按钮(这一点);
        createdButton.setText(ROOMNAME);
        layout.addView(createdButton);
        this.addContentView(布局,layoutParam);

        //如果没有房间让tvnorooms消失

        打破;
    }

}
}
 

解决方案

试试这个:

  //布局上正在工作
    的LinearLayout布局=(的LinearLayout)findViewById(R.id.linear_layout_tags);

    //设置属性按钮
    按钮btnTag =新的按钮(这一点);
    btnTag.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    btnTag.setText(按钮);
    btnTag.setId(some_random_id);

    //添加按钮布局
    layout.addView(btnTag);
 

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.

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.

First Edit (in response to Tanuj's solution)

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>

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

}
}

解决方案

Try this :

    //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);

这篇关于安卓:编程按钮添加到布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆