GridLayout布局

一个不需要适配器的布局空间,安卓3.0以后出来的

效果图如下:


layout布局文件 代码如下:

<?xml version="1.0" encoding="utf-8"?> <!-- GridLayout: 5行 4列 水平布局 --> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:rowCount="5"
    android:columnCount="4" >   <Button
        android:id="@+id/one"
        android:text="1"/>   <Button
        android:id="@+id/two"
        android:text="2"/>    <Button
        android:id="@+id/three"
        android:text="3"/>   <Button
        android:id="@+id/devide"
        android:text="/"/>   <Button
        android:id="@+id/four"
        android:text="4"/>   <Button
        android:id="@+id/five"
        android:text="5"/>   <Button
        android:id="@+id/six"
        android:text="6"/>   <Button
        android:id="@+id/multiply"
        android:text="×"/>   <Button
        android:id="@+id/seven"
        android:text="7"/>   <Button
        android:id="@+id/eight"
        android:text="8"/>   <Button
        android:id="@+id/nine"
        android:text="9"/> <Button
        android:id="@+id/minus"
        android:text="-"/> <Button
        android:id="@+id/zero"
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:text="0"/>   <Button
        android:id="@+id/point"
        android:text="."/> <Button
        android:id="@+id/plus"
        android:layout_rowSpan="2"
        android:layout_gravity="fill"
        android:text="+"/> <Button
        android:id="@+id/equal"
        android:layout_columnSpan="3"
        android:layout_gravity="fill"
        android:text="="/> </GridLayout>




链接地址如下:

http://www.cnblogs.com/skywang12345/p/3154150.html

### GridLayout 布局详解 GridLayout 是一种用于排列视图组件的布局方式,它按照指定的行列数将子视图放置在一个二维网格中[^1]。以下是关于如何使用 `GridLayout` 的具体说明: #### 创建 GridLayout 对象 通过创建一个 `GridLayout` 实例来定义布局结构。可以设置所需的行数 (`rows`) 和列数 (`cols`) 来初始化该对象。 ```java import android.widget.GridLayout; // 定义一个具有 3 行 4 列的 GridLayout GridLayout layout = new GridLayout(context); layout.setRowCount(3); // 设置行数 layout.setColumnCount(4); // 设置列数 ``` #### 添加视图到 GridLayout 中 一旦定义了 `GridLayout`,可以通过调用其 `addView(View child)` 方法向其中添加子视图。这些子视图会被自动分配到下一个可用单元格位置。 ```java Button button = new Button(context); button.setText("Grid Item"); layout.addView(button); ``` #### 解决低版本 SDK 兼容性问题 如果目标设备运行的是较低版本的 Android 系统,则可能无法直接支持 `GridLayout`。此时可通过引入 **Android Support Library** 提供的支持类库解决问题[^2]。确保项目中的 Gradle 文件已包含以下依赖项: ```gradle implementation 'androidx.gridlayout:gridlayout:<version>' ``` 之后即可安全地在旧版系统上使用 `GridLayout` 功能。 #### 属性配置 为了更好地控制布局行为,可调整多种属性参数。例如,利用 `setUseDefaultMargins(boolean useDefaultMargins)` 可启用默认边距;借助 `setAlignmentMode(int alignmentMode)` 能改变对齐模式等。 ```xml <androidx.gridlayout.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" app:columnCount="3" app:rowCount="2"> <!-- 子控件 --> </androidx.gridlayout.widget.GridLayout> ``` 以上 XML 片段展示了如何声明基于 `GridLayout` 的界面设计。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值