Ima*_*min 0 android gravity android-layout layoutparams android-studio
我是android编程的新手。
我只是发现xml不是设置布局的唯一方法。因此,我试图了解以编程方式设置的布局。我一直在尝试更改按钮的位置。我如何设置布局重力,以便将按钮定位在预期的轴上,比方说 bottom和center?
顺便说一句,是icicle同样的事情savedInstanceState?
这是我偶然发现的编码。您能告诉我设置重力的正确方法吗?
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout one = new LinearLayout(this);
myButton1 = new Button1(this);
one.addView(myButton1,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
myButton2 = new Button2(this);
one.addView(mButton2,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
setContentView(one);
Run Code Online (Sandbox Code Playgroud)
我认为,您正在寻找
LinearLayout one = new LinearLayout(this);
one.setGravity(Gravity.BOTTOM | Gravity.CENTER); //to show LinearLayout gravity
Button myButton1 = new Button(this);
one.addView(myButton1,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
myButton1.setText("First");
myButton1.setGravity(Gravity.CENTER); //to show text gravity in button
Button myButton2 = new Button(this);
one.addView(myButton2,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
myButton2.setText("Second");
myButton2.setGravity(Gravity.CENTER); //to show text gravity
setContentView(one);
Run Code Online (Sandbox Code Playgroud)
icicle有时用作参数名称,它只是形式参数之一的占位符onCreate接受一个Bundle参数。该如何称呼取决于您。
LinearLayout的某些属性/sf/answers/1334616601/