概述
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>
实现的效果图如下:
最后
以上就是阳光金鱼为你收集整理的Android约束布局ConstraintLayout实现九宫格的全部内容,希望文章能够帮你解决Android约束布局ConstraintLayout实现九宫格所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复