本文介绍了如何设置layout_gravity编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题很简单,
如何设置我的按钮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);
有关重力值,以及如何设置重力检查
My question is simple,
How to set my buttons layout_gravity programatically?
I found this 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);
Any solution?
解决方案
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编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!