以编程方式初始化和设置Button的参数

时间:2013-11-22 15:39:17

标签: java android layoutparams

我正在尝试以编程方式初始化并设置5个按钮的高度和宽度。下面是我的代码,XML和LogCat。

public class Puzzle extends Activity {

    Button btn1, btn2, btn3, btn4, btn5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.z1to5);

        RelativeLayout relativelayout = (RelativeLayout)findViewById(R.id.relativelayout);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50);

        btn1 = (Button)findViewById(R.id.btn1);
        btn2 = (Button)findViewById(R.id.btn2);
        btn3 = (Button)findViewById(R.id.btn3);
        btn4 = (Button)findViewById(R.id.btn4);
        btn5 = (Button)findViewById(R.id.btn5);

        btn1.setLayoutParams(params);
        btn2.setLayoutParams(params);
        btn3.setLayoutParams(params);
        btn4.setLayoutParams(params);
        btn5.setLayoutParams(params);

        relativelayout.addView(btn1);
        relativelayout.addView(btn2);
        relativelayout.addView(btn3);
        relativelayout.addView(btn4);
        relativelayout.addView(btn5);
    }
}

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/btn1" />

    <Button
        android:id="@+id/btn2" />

    <Button
        android:id="@+id/btn3" />

    <Button
        android:id="@+id/btn4" />

    <Button
        android:id="@+id/btn5" />
</RelativeLayout>

这是我第一次尝试使用Java进行布局。我总是用XML做的。下面是我得到的LogCat错误。

logcat的

11-21 17:22:13.590: E/AndroidRuntime(2639): Process: matt.lyons.tileslider, PID: 2639
11-21 17:22:13.590: E/AndroidRuntime(2639): java.lang.RuntimeException: Unable to start activity ComponentInfo{matt.lyons.tileslider/matt.lyons.tileslider.Puzzle}: java.lang.RuntimeException: Binary XML file line #8: You must supply a layout_width attribute.
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.os.Looper.loop(Looper.java:137)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.app.ActivityThread.main(ActivityThread.java:4998)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at java.lang.reflect.Method.invokeNative(Native Method)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at java.lang.reflect.Method.invoke(Method.java:515)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at dalvik.system.NativeStart.main(Native Method)
11-21 17:22:13.590: E/AndroidRuntime(2639): Caused by: java.lang.RuntimeException: Binary XML file line #8: You must supply a layout_width attribute.
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:492)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5948)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6117)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.widget.RelativeLayout$LayoutParams.<init>(RelativeLayout.java:1225)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:1062)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:82)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:757)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.app.Activity.setContentView(Activity.java:1928)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at matt.lyons.tileslider.Puzzle.onCreate(Puzzle.java:17)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.app.Activity.performCreate(Activity.java:5243)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-21 17:22:13.590: E/AndroidRuntime(2639):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
11-21 17:22:13.590: E/AndroidRuntime(2639):     ... 11 more

我做错了什么?

1 个答案:

答案 0 :(得分:2)

如果要以编程方式创建视图,请不要将方法setContentView()与layoutId参数一起使用,因为它调用LayoutInflater从XML文件创建视图,因为XML不正确而无法执行此操作。所以尝试这样的事情:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout rootLayout = new LinearLayout(getApplicationContext());
    rootLayout.setOrientation(LinearLayout.VERTICAL);

    //Convert DIPs to Pixels
    int dp = 50;
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(px * 4, px);

    for (int i = 1; i < 6; i++) {
        Button button = new Button(getApplicationContext());
        button.setLayoutParams(layoutParams);
        button.setText("Button " + i);
        rootLayout.addView(button);
    }

    setContentView(rootLayout);
}