Skip to content

Instantly share code, notes, and snippets.

@JeffZane
Last active May 23, 2018 12:19
Show Gist options
  • Select an option

  • Save JeffZane/56720f40f8f7eee3071ce07958ce8958 to your computer and use it in GitHub Desktop.

Select an option

Save JeffZane/56720f40f8f7eee3071ce07958ce8958 to your computer and use it in GitHub Desktop.
GridLayout weight
1. 动态创建等分GridLayout,需要用到layout_columnWeight/Height(对应column/RowSpec)属性,注意调用的api(下方note) (bug: 如果child count比coloum count小,UI显示有问题)
for (TopUpProduct.TopUpType.Product product : products) {
TextView textView = (TextView) mInflater.inflate(R.layout.np_item_call_charge, mGridLayout, false);
textView.setText("S$ " + product.getPrice());
mGridLayout.addView(textView);
GridLayout.LayoutParams layoutParams = (GridLayout.LayoutParams) textView.getLayoutParams();
layoutParams.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f); //note: 1f
layoutParams.leftMargin = UiUtils.dp2px(getContext(), 6);
layoutParams.rightMargin = UiUtils.dp2px(getContext(), 6);
layoutParams.topMargin = UiUtils.dp2px(getContext(), 16);
textView.setLayoutParams(layoutParams);
}
2. xml创建等分GridLayout(设置了layout_columnWeight/Height,width一定要设置为0,否则动态改变layout_column时,width会发生改变)
<android.support.v7.widget.GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="14dp"
android:paddingRight="14dp"
app:columnCount="3"
app:orientation="horizontal"
app:rowCount="2"
app:useDefaultMargins="false">
<ImageView
android:id="@+id/iv_singtel"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:background="@drawable/np_sel_bg_operator_logo"
android:scaleType="centerInside"
android:src="@drawable/np_sel_singtel_logo"
app:layout_columnWeight="1"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/iv_starhub"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:background="@drawable/np_sel_bg_operator_logo"
android:scaleType="centerInside"
android:src="@drawable/np_sel_starhub_logo"
app:layout_columnWeight="1"
tools:ignore="ContentDescription,RtlHardcoded" />
<ImageView
android:id="@+id/iv_m1"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:background="@drawable/np_sel_bg_operator_logo"
android:scaleType="centerInside"
android:src="@drawable/np_sel_m1_logo"
app:layout_columnWeight="1"
tools:ignore="ContentDescription,RtlHardcoded" />
<ImageView
android:id="@+id/iv_triangle"
android:layout_width="0dp"
android:layout_height="8dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:src="@drawable/np_ic_triangle"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_row="1"
tools:ignore="ContentDescription,RtlHardcoded" />
</android.support.v7.widget.GridLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment