如何设置layout_gravity programmaticaly? [英] How to set layout_gravity programmaticaly?

查看:107
本文介绍了如何设置layout_gravity programmaticaly?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置layout_gravity编程的按钮?

我发现这个例子在互联网上,但它只是抛出了我一个空指针异常:

 按钮用作MyButton =新的按钮(这一点);

    LinearLayout.LayoutParams LLLP =(LinearLayout.LayoutParams)MyButton.getLayoutParams();
    lllp.gravity = Gravity.RIGHT;
    MyButton.setLayoutParams(LLLP);


    MyLinearLayout.addView(用作MyButton);
 

解决方案

  LinearLayout.LayoutParams PARAMS =新LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT);
params.weight = 1.0F;
params.gravity = Gravity.TOP;

button.setLayoutParams(PARAMS);
 

有关重力值,以及如何设置重力检查重力

How to set layout_gravity programatically for buttons?

I found this example on internet, but its simply throws me a NullPointer exception:

 Button MyButton = new Button(this);

    LinearLayout.LayoutParams  lllp=(LinearLayout.LayoutParams)MyButton.getLayoutParams();
    lllp.gravity=Gravity.RIGHT;
    MyButton.setLayoutParams(lllp); 


    MyLinearLayout.addView(MyButton);

解决方案

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.weight = 1.0f;
params.gravity = Gravity.TOP;

button.setLayoutParams(params);

For gravity values and how to set gravity check Gravity

这篇关于如何设置layout_gravity programmaticaly?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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