简体   繁体   English

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

[英]how set “app:layout_columnWeight” from code?

I am trying to assign the attribute "app:layout_columnWeight" from code. 我正在尝试从代码中分配属性“ app:layout_columnWeight”。 I tried to "GridLayout.LayoutParams" and apply a style to a template. 我尝试使用“ GridLayout.LayoutParams”并将样式应用于模板。 I need to use this attribute because "android:layout_columnWeight" does not work in Api <21. 我需要使用此属性,因为“ android:layout_columnWeight”在Api <21中不起作用。

PS: I'm using android.support.v7.widget.GridLayout not android.widget.GridLayout. PS:我使用的是android.support.v7.widget.GridLayout,而不是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: 主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: 的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: 因此,如果U要使用新的LayoutParams,则每次都使用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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式将app:layout_columnWeight和app:layout_rowWeight设置为GridLayout的子级 - how to set app:layout_columnWeight and app:layout_rowWeight to GridLayout's child programmatically 如何以编程方式添加GridLayout子项的layout_columnWeight? - How to set layout_columnWeight for a GridLayout child when added programmatically? 如何在样式中使用支持 layout_columnWeight? - How to use support layout_columnWeight in a style? 如何以编程方式将 android:layout_columnWeight="1" 设置为 android 支持 v7 Gridlayout 中的元素 - How do I set android:layout_columnWeight="1" programmatically to an element in an android support v7 Gridlayout 如何在 API 21 之前支持 `layout_columnWeight` 和 `layout_rowWeight`? - How to support `layout_columnWeight` and `layout_rowWeight` in pre API 21? 在包“android”中找不到属性“layout_columnWeight”的资源标识符 - No resource identifier found for attribute 'layout_columnWeight' in package 'android' 在 Gridlayout 中的 MaterialCardView 中找不到属性 layout_columnWeight - Attribute layout_columnWeight not found in MaterialCardView inside Gridlayout 如何使用 Java 设置 columnWeight - How to set columnWeight with Java 如何以编程方式设置cardView的columnWeight - How to set columnWeight of cardView programmatically 以编程方式设置ColumnSpan和ColumnWeight - Set ColumnSpan and ColumnWeight programmatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM