Android GridLayout 动态添加子控件 + 平均分配空间

有时候会遇到这样的需求:
1. 要求子控件网格布局,平均分布
2. 内容根据接口动态加载
3. 父控件充满界面剩余空间,不可滑动

看到1和2的时候GridView和RecyclerView都能胜任,但是不可滑动这个需求就需要对他们做特殊处理,并且不容易做分辨率适配,这时候用GridLayout动态加载做起来更简单。

具体步骤

导入v7包的GridLayout

由于原生GridLayout平均分配子布局要求sdk21+,因此先导入v7包下的GridLayout

compile 'com.android.support:gridlayout-v7:25.3.1'

设置GridLayout布局

下面的布局需要一个自定义轮播图和2个GridLayout按比例分配屏幕空间。
使用app:columnCountapp:rowCount属性定义列数和行数。
使用weight属性时,使用0dp设置动态计算的宽度或高度来进行优化。

<LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">        <FrameLayout            android:id="@+id/viewpager_container"            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_marginBottom="@dimen/dp_6"            android:layout_weight="1.2" />        <android.support.v7.widget.GridLayout            android:id="@+id/function_grid"            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_marginBottom="@dimen/dp_6"            android:layout_weight="1"            app:columnCount="3"            app:rowCount="2" />        <android.support.v7.widget.GridLayout            android:id="@+id/industry_grid"            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="2"            app:columnCount="3"            app:rowCount="3" />    LinearLayout>

平均分配空间

如果你只想要子布局平均分配空间即可,那么可以在xml布局中如下设置。
子控件通过app:layout_rowWeightapp:layout_columnWeight声明自己在布局中占据的比重,都设置为1即可平均分布。
子控件的width和height可以wrap_content也可以0dp,但是不可以match_parent

<android.support.v7.widget.GridLayout            android:id="@+id/function_grid"            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_marginBottom="@dimen/dp_6"            android:layout_weight="1"            app:columnCount="3"            app:rowCount="2" >            <View                android:layout_width="0dp"                android:layout_height="0dp"                app:layout_row="0"                app:layout_column="0"                app:layout_rowWeight="1"                 app:layout_columnWeight="1"/>            <View                android:layout_width="0dp"                android:layout_height="0dp"                app:layout_row="0"                app:layout_column="1"                app:layout_rowWeight="1"                app:layout_columnWeight="1"/>            <View                android:layout_width="0dp"                android:layout_height="0dp"                app:layout_row="0"                app:layout_column="2"                app:layout_rowWeight="1"                app:layout_columnWeight="1"/>            <View                android:layout_width="0dp"                android:layout_height="0dp"                app:layout_row="1"                app:layout_column="0"                app:layout_rowWeight="1"                app:layout_columnWeight="1"/>            <View                android:layout_width="0dp"                android:layout_height="0dp"                app:layout_row="1"                app:layout_column="1"                app:layout_rowWeight="1"                app:layout_columnWeight="1"/>        android.support.v7.widget.GridLayout>

动态添加子控件

for (int i = 0, j = list.size(); i < j; i++) {    ......    View functionView = new View(getContext());    functionView.setBackgroundResource(iconResId);    ......    //使用Spec定义子控件的位置和比重    GridLayout.Spec rowSpec = GridLayout.spec(i / 3, 1f);    GridLayout.Spec columnSpec = GridLayout.spec(i % 3, 1f);    //将Spec传入GridLayout.LayoutParams并设置宽高为0,必须设置宽高,否则视图异常    GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams(rowSpec, columnSpec);    layoutParams.height = 0;    layoutParams.width = 0;    //还可以根据位置动态定义子控件直接的边距,下面的意思是    //第一行的子控件都有2dp的bottomMargin,中间位置的子控件都有2dp的leftMargin和rightMargin    if (i / 3 == 0)        layoutParams.bottomMargin = getResources().getDimensionPixelSize(R.dimen.dp_2);    if (i % 3 == 1) {        layoutParams.leftMargin = getResources().getDimensionPixelSize(R.dimen.dp_2);        layoutParams.rightMargin = getResources().getDimensionPixelSize(R.dimen.dp_2);    }    functionGrid.addView(functionView, layoutParams);}

效果

更多相关文章

  1. Android中常用布局单位
  2. Android番外篇——XML layout与CSS 转载
  3. Android(安卓)M 动态权限获取
  4. Android基础控件之基本属性
  5. android listview 中 item显示表格样式
  6. Android(安卓)布局中长度单位的深入研究
  7. 【Android】Android实现自定义带文字和图片的Button
  8. android 简析自定义布局、布局的执行流程(http://blog.sina.com.c
  9. Android(安卓)Studio VS Eclipse (还在用Eclipse?你OUT了!)

随机推荐

  1. android:WALLPAPER_CHANGED不适用于三星Ga
  2. Android NFC 近场通讯开发全解
  3. 通过不在android游标中工作的顺序
  4. Android全屏模式,沉浸模式。粘性沉浸模式
  5. 制作动态表格(最好的方法)
  6. Android短彩信源码解析-短信发送流程(三)
  7. android app启动就闪退怎么办?
  8. 如何设计数据库模型来记录客户的历史活动
  9. Android百度地图定位定到了海里,定到了非
  10. android插件化-apkplug中以监听方式获取O