![](/img/trans.png)
[英]how to set app:layout_columnWeight and app:layout_rowWeight to GridLayout's child programmatically
[英]how set “app:layout_columnWeight” from code?
我正在尝试从代码中分配属性“ 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);
}
}
我也有问题,我的解决方法是:
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);
试试吧:
((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));
如果有人想要询问我的代码,我使用“ RecyclerView”解决了问题,效率更高。
在这之后
grid.addView(newMonster);
加
((android.support.v7.widget.GridLayout.LayoutParams) newMonster.getLayoutParams()).columnSpec = android.support.v7.widget.GridLayout.spec(GridLayout.UNDEFINED, 1, 1f);
因此,如果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.