Android约束布局ConstraintLayout实现九宫格

12 篇文章 0 订阅
1 篇文章 0 订阅

Android约束布局ConstraintLayout实现九宫格

这个实现是不能动态添加项目的,只是固定项目的九宫格,如果要动态增加item的最好还是使用GridLayout。不多说了直接上代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MenuItemActivity"
    tools:layout_editor_absoluteY="81dp">

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_width="0dp"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/imageButton"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="16dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/imageButton2"
        app:layout_constraintLeft_toLeftOf="@id/imageButton2"
        app:layout_constraintRight_toRightOf="@id/imageButton2"
        android:text="@string/tv_item"
        />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="16dp"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintLeft_toRightOf="@id/imageButton2"
        app:layout_constraintRight_toLeftOf="@id/imageButton3"
        app:srcCompat="@mipmap/ic_launcher" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/imageButton"
        app:layout_constraintLeft_toLeftOf="@id/imageButton"
        app:layout_constraintRight_toRightOf="@id/imageButton"
        android:text="@string/tv_item"
        />

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginRight="16dp"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintLeft_toRightOf="@id/imageButton"
        app:layout_constraintRight_toRightOf="parent"
        app:srcCompat="@mipmap/ic_launcher" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/imageButton3"
        app:layout_constraintLeft_toLeftOf="@id/imageButton3"
        app:layout_constraintRight_toRightOf="@id/imageButton3"
        android:text="@string/tv_item"
        />

    <ImageButton
        android:id="@+id/imageButton8"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@mipmap/ic_launcher"
        app:layout_constraintTop_toBottomOf="@id/imageButton2"
        android:layout_marginTop="32dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/imageButton8"
        app:layout_constraintLeft_toLeftOf="@id/imageButton8"
        app:layout_constraintRight_toRightOf="@id/imageButton8"
        android:text="@string/tv_item"
        />

    <ImageButton
        android:id="@+id/imageButton9"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintTop_toBottomOf="@id/imageButton"
        app:layout_constraintLeft_toRightOf="@id/imageButton2"
        app:layout_constraintLeft_toLeftOf="@id/imageButton"
        android:layout_marginTop="32dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/imageButton9"
        app:layout_constraintLeft_toLeftOf="@id/imageButton9"
        app:layout_constraintRight_toRightOf="@id/imageButton9"
        android:text="@string/tv_item"
        />

    <ImageButton
        android:id="@+id/imageButton10"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintTop_toBottomOf="@id/imageButton3"
        app:layout_constraintLeft_toRightOf="@id/imageButton"
        app:layout_constraintLeft_toLeftOf="@id/imageButton3"
        android:layout_marginTop="32dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/imageButton10"
        app:layout_constraintLeft_toLeftOf="@id/imageButton10"
        app:layout_constraintRight_toRightOf="@id/imageButton10"
        android:text="@string/tv_item"
        />

</android.support.constraint.ConstraintLayout>

实现的效果图如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值