星空网 > 软件开发 > 操作系统

android GridLayout布局

  android4.0版本后新增了一个GridLayout,它使用虚细线将布局划分为行、列和单元格,也支持一个控件在行、列上都有交错排列,其实用方法和LinearLayout,Relativelayout等类似,只不过多了一些特有的属性。

GridLayout的布局策略简单分为以下三个部分:

  首先它与LinearLayout布局一样,也分为水平和垂直两种方式,默认是水平布 局,一个控件挨着一个控件从左到右依次排列,但是通过指定android:columnCount设置列数的属性后,控件会自动换行进行排列。另一方面, 对于GridLayout布局中的子控件,默认按照wrap_content的方式设置其显示,这只需要在GridLayout布局中显式声明即可。

 

 

      其次,若要指定某控件显示在固定的行或列,只需设置该子控件的android:layout_row和android:layout_column属性即 可,但是需要注意:android:layout_row=”0”表示从第一行开始,android:layout_column=”0”表示从第一列开 始,这与编程语言中一维数组的赋值情况类似。

 

 

       最后,如果需要设置某控件跨越多行或多列,只需将该子控件的android:layout_rowSpan或者layout_columnSpan属性设置为数值,再设置其layout_gravity属性为fill即可,前一个设置表明该控件跨越的行数或列数,后一个设置表明该控件填满所跨越的整行或整列。

  可以说使用GridLayout以后可以完全不用tablelayout了,而且使用GridLayout有效减少了布局的深度,提高了app整体的性能质量。

  下面是使用GridLayout完成的效果图:

  android GridLayout布局images/loading.gif' data-original="http://images2015.cnblogs.com/blog/828272/201512/828272-20151216131622099-1825458958.png" width="427" height="174" />

  布局代码如下:

<?  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:background="@drawable/digital_bg"  android:columnCount="6"  android:orientation="horizontal"  android:padding="16dip"  android:rowCount="3">  <TextView    android:id="@+id/edit_input"    android:layout_columnSpan="5"    android:layout_gravity="fill"    android:textSize="14sp"    android:gravity="center_vertical"    android:paddingLeft="8dip"    android:background="@drawable/input_bg" />  <ImageButton    android:id="@+id/delete"    android:layout_marginLeft="16dip"    android:background="@drawable/delete_btn_style" />       <ImageButton    android:id="@+id/num_1"    android:layout_marginTop="16dip"    android:background="@drawable/num_1_btn_style" />  <ImageButton    android:id="@+id/num_2"    android:layout_marginLeft="16dip"    android:layout_marginTop="16dip"    android:background="@drawable/num_2_btn_style" />  <ImageButton    android:id="@+id/num_3"    android:layout_marginLeft="16dip"    android:layout_marginTop="16dip"    android:background="@drawable/num_3_btn_style" />  <ImageButton    android:id="@+id/num_4"    android:layout_marginLeft="16dip"    android:layout_marginTop="16dip"    android:background="@drawable/num_4_btn_style" />  <ImageButton    android:id="@+id/num_5"    android:layout_marginLeft="16dip"    android:layout_marginTop="16dip"    android:background="@drawable/num_5_btn_style" />      <ImageButton    android:id="@+id/confirm"    android:layout_marginLeft="16dip"    android:layout_marginTop="16dip"    android:layout_rowSpan="2"    android:background="@drawable/confirm_btn_style" />  <ImageButton    android:id="@+id/num_6"    android:layout_marginTop="16dip"    android:background="@drawable/num_6_btn_style" />  <ImageButton    android:id="@+id/num_7"    android:layout_marginLeft="16dip"    android:layout_marginTop="16dip"    android:background="@drawable/num_7_btn_style" />  <ImageButton    android:id="@+id/num_8"    android:layout_marginLeft="16dip"    android:layout_marginTop="16dip"    android:background="@drawable/num_8_btn_style" />  <ImageButton    android:id="@+id/num_9"    android:layout_marginLeft="16dip"    android:layout_marginTop="16dip"    android:background="@drawable/num_9_btn_style" />  <ImageButton    android:id="@+id/num_0"    android:layout_marginLeft="16dip"    android:layout_marginTop="16dip"    android:background="@drawable/num_0_btn_style" /></GridLayout>

 

 

 

参考资料:http://blog.csdn.net/pku_android/article/details/7343258

 




原标题:android GridLayout布局

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

TikTok跨境电商:Tk需要满足什么条件才可以开直播?:https://www.goluckyvip.com/news/9984.html
继TikTok之后,中国电商平台Shein和Temu又遭美方围堵! :https://www.goluckyvip.com/news/9985.html
美国物流市场严查违规行为,部分违法人员已被调查:https://www.goluckyvip.com/news/9986.html
TIKTOK跨境MCN怎么提现? :https://www.goluckyvip.com/news/9987.html
拼多多“不要”Temu了:https://www.goluckyvip.com/news/9988.html
TikTok热度超10亿,“彩虹圈玩具”单个视频获6000万播放:https://www.goluckyvip.com/news/9989.html
深圳玉龙公园簕杜鹃花海花期时间+门票+交通:https://www.vstour.cn/a/365174.html
出境旅游预订须知:https://www.vstour.cn/a/365175.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流