15、Android -- GridLayout布局 基础学习

常用属性/设置

父控件属性

  • android:orientation:设置水平显示还是垂直显示(值:horizontal–水平;vertical–垂直)
  • android:columnCount:设置行的显示个数(针对水平布局,即orientation值为horizontal)
  • android:rowCount:设置列的显示个数(针对垂直布局,即orientation值为vertical)

子控件属性

  • android:layouy_column:显示在第几列
  • android:layout_columnSpan:横向跨几列
  • androld:layout_columnWeight:横向剩余空间分配方式
  • android:layout_gravity:在网格中的显示位置
  • android:layout_row:显示在第几行
  • android:layout_rowSpan:横向跨几行
  • androld:layout_rowWeight:纵向剩余空间分配方式

示例

1、res/layout/activity_grid_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:columnCount="4"
    tools:context=".GridLayoutActivity2">
    <Button
        android:text="第一个按钮"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <Button
        android:text="第二个按钮"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="1"
        android:layout_column="0"
        />
    <Button
        android:text="第三个按钮"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="0"
        android:layout_column="1"
        />
    <Button
        android:text="第四个按钮"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        />
    <Button
        android:text="第五个按钮"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <Button
        android:text="第六个按钮"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        />
</GridLayout>

效果图

GridLayout效果图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用ListView实现的GridView效果,其中每个子元素可以设置自己的占位,比如当前元素占几行几列(rowSpancolumnSpan),所以看起来就像一个不规则的随机的网格布局。项目地址:https://github.com/felipecsl/AsymmetricGridView 效果图:使用说明:xml<com.felipecsl.asymmetricgridview.library.widget.AsymmetricGridView     xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@ id/listView"     android:layout_width="match_parent"     android:layout_height="match_parent"/>activity中:@Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);     listView = (AsymmetricGridView) findViewById(R.id.listView);     // Choose your own preferred column width     listView.setRequestedColumnWidth(Utils.dpToPx(this, 120));     final List<AsymmetricItem> items = new ArrayList<>();     // initialize your items array     adapter = new ListAdapter(this, listView, items);     listView.setAdapter(adapter); }支持追加更多的元素:// Will append more items at the end of the adapter. listView.getAdapter().appendItems(moreItems); // resetting the adapter items. Will clear the adapter // and add the new items. listView.getAdapter().setItems(items);设置是否重新排列达到更好的显示效果:// Setting to true will move items up and down to better use the space // Defaults to false. listView.setAllowReordering(true); listView.isAllowReordering(); // true设置item的占位:item 一般这样定义:public DemoItem(final int columnSpan, final int rowSpan, int position) {     this.columnSpan = columnSpan;     this.rowSpan = rowSpan;     this.position = position; }columnSpan 和rowSpan分别代表列占位和行占位。说明:目前当item的rowSpan = 2 columnSpan = 2时可以达到最佳的状态。这个后续会继续改进。item的大小越统一,效率越高,特殊大小的元素少于20%是比较理想的状态。不然没法在不预留很多空位的情况下,合理的显示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值