android ConstraintLayout 局中

2022-07-07  本文已影响0人  程序狮

居中

1、父容器的居中

image.png
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/size_dp_80"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/bg_white_cir">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginLeft="@dimen/size_dp_30"
        android:src="@drawable/icon_home_fixed"
        />

</android.support.constraint.ConstraintLayout>

2、多个控件居中

image.png

实现思路是在链头加上 类似代码,让脑袋中抽象的链条 用constraintLayout的chain属性实现

app:layout_constraintHorizontal_chainStyle="packed"

chain 除了链条方向有横向和竖向区分外, chain链条上的模式有 3种

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/size_dp_80"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@drawable/bg_white_cir">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/textView"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_chainStyle="packed"
        android:layout_marginLeft="@dimen/size_dp_30"
        android:src="@drawable/icon_home_fixed"
        />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/image"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:text="Hello Word"
        />

</android.support.constraint.ConstraintLayout>
上一篇 下一篇

猜你喜欢

热点阅读