简体   繁体   中英

how set “app:layout_columnWeight” from code?

I am trying to assign the attribute "app:layout_columnWeight" from code. I tried to "GridLayout.LayoutParams" and apply a style to a template. I need to use this attribute because "android:layout_columnWeight" does not work in Api <21.

PS: I'm using android.support.v7.widget.GridLayout not android.widget.GridLayout.

I attached the picture:

Thanks for your help.

EDIT: not exactly, I've tried many things ...

Layout-frame template (sample):

<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>

Frame style:

<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>

Grid on main XML layout:

    <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);
    }

}

I have the problem too, my solution is:

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);

Try it:

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

if view have been add to layout

or in your code:

replace

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

with

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

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

after this

grid.addView(newMonster);

add

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

So if U want use new LayoutParams just create it every time with params:

   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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM