![](/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.