Sunday, February 9, 2014

How to dynamically add custom Button


            
No configuration
inside the xmlLike follows
 
public class AddButton extends Activity {
/ ** Called when the activity is first created. * /
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
/ / SetContentView (R.layout.main);
final LinearLayout layout2 = new LinearLayout (this);
layout2.setOrientation (LinearLayout.VERTICAL);
Button bt1 = new Button (this);
setContentView (layout2);
Button bt2 = new Button (this);
bt1.setText ("Button 1");
bt2.setText ("Button 2");
layout2.addView (bt1);
layout2.addView (bt2);
setContentView (layout2);
}}


But how to add a custom button? ?
Reply:
How do you want to customize Act
Reply:
Custom button is nothing more than set the background, set the size.
 ViewGroup.LayoutParams params = new ViewGroup.LayoutParams (
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
bt1.setBackgroundResource (R.drawable.xx);
bt1.setGravity (Gravity.CENTER);
layout2.addView (root, params);

The landlord of the first sentence setContentView (layout2); delete

No comments:

Post a Comment