动态设置android中按钮的宽度和高度

时间:2014-04-24 06:30:00

标签: java android eclipse

我在桌子内有一个按钮,其文字在'购买礼物'之间变化。和'缺货'取决于数据库中的两种情况。所以我想在代码中动态地将width和height设置为wrap_content。

在班级,我宣布了以下两行:

LinearLayout.LayoutParams lp2;
lp2=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1);

我使用了一种方法来动态设置宽度和高度:

public void giftdetailssetting(String gift_name)
    {
        //btnbuygift.setWidth(200);
        btnbuygift.setText("Buy Gift");
        //btnbuygift.setEnabled(true);
        btnbuygift.setClickable(true);
        btnbuygift.setLayoutParams(lp2);

        // btnbuygift.setHeight(200);
        gift_src=dm.retrievegiftsrc(gift_name);
        Resources res = getResources();
        int resId=res.getIdentifier(gift_src,"drawable","com.example.shopkart");
        iv_gift.setImageResource(resId);

        List<String> giftvalues=dm.getgiftdetails(gift_name);
        giftprice=(String)giftvalues.get(1);
        txtgiftname.setText((String)giftvalues.get(0));
        txtgiftprice.setText("Rs "+giftprice);
        txtgiftquantity.setText((String)giftvalues.get(2));
        if(Integer.parseInt((String)giftvalues.get(2))==0)
        {
            btnbuygift.setText("OUT OF STOCK");
            btnbuygift.setLayoutParams(lp2);
            //btnbuygift.setEnabled(false);
            btnbuygift.setClickable(false);
        }

    }

但是我在logcat中遇到以下错误:

04-24 14:57:00.240: E/AndroidRuntime(26781): FATAL EXCEPTION: main
04-24 14:57:00.240: E/AndroidRuntime(26781): Process: com.example.shopkart, PID: 26781
04-24 14:57:00.240: E/AndroidRuntime(26781): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.TableRow$LayoutParams
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.widget.TableRow.getColumnsWidths(TableRow.java:299)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.widget.TableLayout.findLargestCells(TableLayout.java:508)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.widget.TableLayout.measureVertical(TableLayout.java:473)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.widget.TableLayout.onMeasure(TableLayout.java:439)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.View.measure(View.java:16497)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.View.measure(View.java:16497)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.View.measure(View.java:16497)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.View.measure(View.java:16497)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.View.measure(View.java:16497)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.Choreographer.doFrame(Choreographer.java:544)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.os.Handler.handleCallback(Handler.java:733)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.os.Handler.dispatchMessage(Handler.java:95)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.os.Looper.loop(Looper.java:136)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.app.ActivityThread.main(ActivityThread.java:5017)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at java.lang.reflect.Method.invokeNative(Native Method)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at java.lang.reflect.Method.invoke(Method.java:515)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at dalvik.system.NativeStart.main(Native Method)

如何在两种情况下动态设置按钮的宽度和高度作为换行内容?PLS HELP !!

4 个答案:

答案 0 :(得分:0)

您收到ClassCastException,因为您要将LinearLayout.LayoutParam分配给Button类型的TableRow

更改

LinearLayout.LayoutParams lp2;
lp2=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1);

TableRow.LayoutParams lp2;
lp2=new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1);

不要忘记为LayoutParams.WRAP_CONTENT导入正确的软件包,否则您将再次遇到相同的错误。

答案 1 :(得分:0)

我希望当您尝试使用LayoutParams时,您的问题将得到解决;使用这个我们可以在android中改变按钮动态的大小!

例如;

button.setLayoutParams (new LayoutParams(50, LayoutParams.WRAP_CONTENT)

希望它会对你有所帮助!

答案 2 :(得分:0)

您可以使用以下代码设置宽度和高度:

Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tblRow.addView(btnTag);

答案 3 :(得分:0)

看看你的logcat很明显你在你的情况下将LayoutParams设置为Linear布局而不是Tablerow。

但根据文档,Table行的子节点采用MATCH_PARENT作为宽度。因此,如果您在其中嵌入一个带有按钮的水平线性布局会更好,然后可以使用您的代码本身调整大小。