如何从代码中设置“app:layout_columnWeight”?

时间:2015-11-18 19:45:10

标签: android android-layout

我正在尝试从代码中分配属性“app:layout_columnWeight”。我尝试了“GridLayout.LayoutParams”并将样式应用于模板。我需要使用这个属性,因为“android:layout_columnWeight”在Api< 21中不起作用。

PS:我使用的是android.support.v7.widget.GridLayout而不是android.widget.GridLayout。

我附上了照片:

感谢您的帮助。

编辑:不完全是,我尝试了很多东西......

布局框架模板(样本):

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/fonfo_monstruo"
    style="@style/monstruo">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="none"
        android:id="@+id/textView"
        android:layout_gravity="center"
        android:textColor="#ffffff" />
 </FrameLayout>

框架风格:

<style name="monstruo"  >
<item name="android:layout_margin" >5dp</item>
<item xmlns="http://schemas.android.com/apk/res-auto" name="layout_columnWeight" >1</item>
</style>

主XML布局上的网格:

    <android.support.v7.widget.GridLayout
    app:orientation="horizontal"
    android:isScrollContainer="true"
    android:id="@+id/grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:columnCount="3"
    android:background="#cbb5ff"
    android:layout_marginBottom="100dp" />

的onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    android.support.v7.widget.GridLayout grid= (GridLayout) findViewById(R.id.grid);
    android.support.v7.widget.GridLayout.LayoutParams params=new android.support.v7.widget.GridLayout.LayoutParams();
    params.height=200;
    params.width=0;

    for (int i = 0; i < 10; i++) {

        View newMonster=View.inflate(this,R.layout.layu_mosnter,null);
        newMonster.setLayoutParams(params);
        grid.addView(newMonster);
    }

}

4 个答案:

答案 0 :(得分:1)

试一试:

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

如果已将视图添加到布局

或在您的代码中:

替换

 android.support.v7.widget.GridLayout.LayoutParams params=new android.support.v7.widget.GridLayout.LayoutParams();

GridLayout.LayoutParams param = new LayoutParams(GridLayout.spec(GridLayout.UNDEFINED, 1f),  GridLayout.spec(GridLayout.UNDEFINED, 1f));

答案 1 :(得分:1)

我也有问题,我的解决方案是:

GridLayout.Spec spec = GridLayout.spec(GridLayout.UNDEFINED, 1.0f);
GridLayout.LayoutParams lp = new GridLayout.LayoutParams(new MarginLayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT));
ItemView itemView = newChildView(mGridLayout);
lp.columnSpec = spec;
mGridLayout.addView(itemView, lp);

答案 2 :(得分:0)

如果有人想要问我的代码,我使用“RecyclerView”解决了这个问题的效率更高。

答案 3 :(得分:0)

之后

((android.support.v7.widget.GridLayout.LayoutParams) newMonster.getLayoutParams()).columnSpec = android.support.v7.widget.GridLayout.spec(GridLayout.UNDEFINED, 1, 1f);

添加

   import android.support.v7.widget.GridLayout;
    ...
    for (int i = 0; i < 10; i++) {
        View newMonster = View.inflate(this, R.layout.frame, null);
        GridLayout.LayoutParams params = new GridLayout.LayoutParams(GridLayout.spec(GridLayout.UNDEFINED), GridLayout.spec(GridLayout.UNDEFINED, 1, 1f));
        params.height = 200;
        params.width = 0;
        newMonster.setLayoutParams(params);
        grid.addView(newMonster);
    }

因此,如果你想使用新的LayoutParams,只需每次使用params创建它:

{{1}}