How do I set android:layout_columnWeight=“1” programmatically to an element in an android support v7 Gridlayout

送分小仙女□ 提交于 2019-12-04 22:16:30

I found this method from GridLayout: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.0_r1/android/widget/GridLayout.java#GridLayout.spec%28int%2Cfloat%29

So maybe you could try something like this:

((GridLayout.LayoutParams) this.getLayoutParams()).columnSpec =
    GridLayout.spec(GridLayout.UNDEFINED, 1f);

-Here you are ! :>

Button button = new Button(this);
GridLayout.LayoutParams param= new GridLayout.LayoutParams(GridLayout.spec(
            GridLayout.UNDEFINED,GridLayout.FILL,1f),
            GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f));
param.height = 0;                                                                                                           
param.width = 0;
button.setLayoutParams(param);

This functions of GridLayout.Spec will allow you to set the weight correctly:

public static Spec spec(int start, int size, Alignment alignment, float weight)
public static Spec spec(int start, Alignment alignment, float weight)
public static Spec spec(int start, int size, float weight) 
public static Spec spec(int start, float weight)

usage example:

((LayoutParams) gm.getViewFromGridCoords(i, j).getLayoutParams()).columnSpec = GridLayout.spec(j, span, CENTER, 1);

For anyone using xamarin.android, these answers helped me, but I had to change the syntax a bit. So if anyone else is using xamarin.android here is the syntax:

GridLayout.LayoutParams param = new GridLayout.LayoutParams();
param.RowSpec = GridLayout.InvokeSpec(GridLayout.Undefined, 1f);
param.ColumnSpec = GridLayout.InvokeSpec(GridLayout.Undefined, 1f);
view.LayoutParameters = param;

Where view is the view contained in the GridLayout.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!