How do I create a button programatically?

梦想的初衷 提交于 2019-12-13 07:17:34

问题


I just want to dynamicly add buttons to my Layout when i want to. The buttons should be like this XML Button:

 <Button android:text="Text" 
 android:gravity="bottom" 
 android:textSize="10dp" 
 android:textColor="#FFFFFF" 
 android:layout_width="wrap_content"
 android:background="@drawable/attack1"
 android:layout_height="wrap_content" 
 android:id="@+id/workingButton">
 </Button>

.

public class GravityIssueActivity extends Activity
{
    LinearLayout layout;
    Button newButton;
    Button buttonByXml;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //the button in the xml file
        buttonByXml = (Button)findViewById(R.id.workingButton);
        layout = (LinearLayout)findViewById(R.id.layoutToInsert);
        //my new programatically "born" button
        newButton = new Button(this);
        //Setting the properties as i want
        newButton.setText("Text");
        newButton.setTextSize(10);
        newButton.setTextColor(Color.WHITE);
        newButton.setBackgroundResource(R.drawable.attack1);
        // Gravity = Bottom !!!!!!!!!!
        newButton.setGravity(Gravity.BOTTOM);
        // getting the XML buttons params just for case...
        newButton.setLayoutParams(new LayoutParams(buttonByXml.getLayoutParams()));
        //Adding my new Button to the layout
        layout.addView(newButton);
    }
}

And here is an image of the results:

How is it possible to became different result when I copy all the attributes?


回答1:


If you want to create dynamic view (like Button,textview etc) then just use this code and run it in your application.

MyActivity.java://your java file

 LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);
 Button btn = new Button(this)
 btn.setText("My Dynamic Button);
 btn.setMinLines(1);
 btn.setMaxLines(3);
 ll.addView(et);

In XML File:

 <LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/TextView01"
android:layout_below="@+id/relativeLayout1"
android:orientation="vertical" >




回答2:


You can absolutely create buttons in code but it's not considered a best-practice unless you have a good reason for dynamically creating the controls. Check out this post Add an array of buttons to a GridView in an Android application.




回答3:


Try using

 Button b = new Button();

This gives you a View instance that can be added to your current parent activity or fragmnet view. For a full reference of possible settings look at http://developer.android.com/reference/android/widget/Button.html

You can use all the set methods provided by parent views in the object hierarchy.




回答4:


If you need to align text to the bottom of the button, all you need is:

Button button = ...
//apply required paramteres
button.setGravity(Gravity.BOTTOM);



回答5:


use below code.you also add other parameters

    Button submit=new Button(this);
    LinearLayout.LayoutParams params= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    params.setMargins(25, 0, 25, 0);
    submit.setLayoutParams(params);
    submit.setText("Attack");
    submit.setTextSize(10);
    submit.setTextColor(getResources().getColor(R.color.white));
    submit.setBackgroundResource(R.drawable.attack);


来源:https://stackoverflow.com/questions/8079799/how-do-i-create-a-button-programatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!