如何以编程方式设置layout_gravity?

本文翻译自:How to set layout_gravity programmatically?

My question is simple, 我的问题很简单,

How to set my buttons layout_gravity programmatically? 如何以编程方式设置我的按钮layout_gravity?

I found this on internet, but it simply throws me a Nullpointer exception: 我在互联网上发现了这个,但它只是抛出了一个Nullpointer异常:

 Button MyButton = new Button(this);

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


    MyLinearLayout.addView(MyButton);

Any solution? 有解决方案吗


#1楼

参考:https://stackoom.com/question/Xm4a/如何以编程方式设置layout-gravity


#2楼

This question is old but I just had the same problem and solved it like this 这个问题很老,但我遇到了同样的问题并且像这样解决了

LayoutParams lay = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)
lay.gravity = Gravity.CENTER;

#3楼

I'd hate to be resurrecting old threads but this is a problem that is not answered correctly and moreover I've ran into this problem myself. 我不想复活旧线程,但这是一个无法正确回答的问题,而且我自己遇到了这个问题。

Here's the long bit, if you're only interested in the answer please scroll all the way down to the code: 这里有点长,如果你只对答案感兴趣,请一直向下滚动到代码:

android:gravity and android:layout_gravity works differently. android:gravity和android:layout_gravity的工作方式不同。 Here 's an article I've read that helped me. 是我读过的一篇帮助我的文章。

GIST of article: gravity affects view after height/width is assigned. 文章的GIST:重力影响高度/宽度分配后的视图。 So gravity centre will not affect a view that is done FILL_PARENT (think of it as auto margin). 因此,重心不会影响完成FILL_PARENT的视图(将其视为自动边距)。 layout_gravity centre WILL affect view that is FILL_PARENT (think of it as auto pad). layout_gravity center将影响FILL_PARENT视图(将其视为自动填充)。

Basically, android:layout_gravity CANNOT be access programmatically, only android:gravity. 基本上,android:layout_gravity不能以编程方式访问,只有android:gravity。 In the OP's case and my case, the accepted answer does not place the button vertically centre. 在OP的情况和我的情况下,接受的答案不会将按钮垂直居中。

To improve on Karthi's answer: 改进Karthi的答案:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
button.setLayoutParams(params);

Link to LinearLayout.LayoutParams . 链接LinearLayout.LayoutParams

android:layout_gravity shows "No related methods" meaning cannot be access programatically. android:layout_gravity显示“无相关方法”意思无法以编程方式访问。 Whereas gravity is a field in the class. 而重力是班上的一个领域。


#4楼

I had a similar problem with programmatically setting layout_gravity on buttons in a GridLayout. 我在GridLayout中按钮上以编程方式设置layout_gravity时遇到了类似的问题。

The trick was to set gravity on the button layoutParams AFTER the button was added to a parent (GridLayout), otherwise the gravity would be ignored . 诀窍是在按钮被添加到父(GridLayout) 之后在按钮layoutParams上设置重力, 否则将忽略重力

grid.addView(button)
((GridLayout.LayoutParams)button.getLayoutParams()).setGravity(int)

#5楼

If you want to put a view in the center of parent, you can do with following code.. 如果要将视图放在父级的中心,可以使用以下代码。

public class myLayout extends LinearLayout {

    public myLayout(Context context) {
      super(context);

      RelativeLayout vi = (RelativeLayout) ((LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
            R.layout.activity_main, null);
      LinearLayout.LayoutParams cc = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
      cc.gravity = Gravity.CENTER;
      this.setGravity(Gravity.CENTER);
      this.addView(vi);
  }
}

these code section make LinearLayout put the first view elements in the center of parent. 这些代码部分使LinearLayout将第一个视图元素放在父级的中心。 So, we system don't consider the initial width and high to arrange view in the center . 因此,我们系统不考虑初始宽度和高度来排列中心的视图。 I do the code section well. 我很好地完成了代码部分。


#6楼

I use someting like that: (Xamarin and C# code) 我喜欢这样:( Xamarin和C#代码)

  LinearLayout linLayout= new LinearLayout(this); 
    linLayout.SetGravity(GravityFlags.Center);

    TextView txtView= new TextView(this);  
     linLayout.AddView(txtView); 

the SetGravity puts my textView in the center of the layout. SetGravity将我的textView放在布局的中心。 So SetGravity layout property refer to layout content 所以SetGravity布局属性指的是布局内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值