在Android中以编程方式更改Button的宽度 [英] Programmatically change the width of the Button in android

查看:218
本文介绍了在Android中以编程方式更改Button的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式更改Buttonwidth.我可以更改height,但宽度不变.以下是我的代码段

How to change the width of a Button programmatically. I am able to change the height but the width does not change. Following is my code snippet

private void createButton(final String label)
{

    Button button = new Button(this);
    button.setText(label);


    button.setWidth(10);
    button.setHeight(100);

    button.setOnClickListener(new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {


        }
    });
    mMenuContainer.addView(button, mMenuItemLayoutParamters);


}

但是Buttonwidth占据了屏幕的width.

推荐答案

button.setLayoutParams(new LinearLayout.LayoutParams(10, 100));

应该为您工作.

对此有一个很棒的示例在线:检查这个

There is one Awesome Example online for this : check this

您应该应用包含ViewViewGroup中的LayoutParams.也就是说,如果您的按钮位于RelativeLayout内,则应使用RelativeLayout.LayoutParams

you should apply LayoutParams of the ViewGroup that contains your View. That is, if your button is inside RelativeLayout, you should use RelativeLayout.LayoutParams

这篇关于在Android中以编程方式更改Button的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆