IT虾米网

Android ConstraintLayout约束布局的居中

mate10pro 2018年05月31日 手机开发 4483 0

Android ConstraintLayout约束布局的居中

xml代码:

<?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" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
 
    <TextView 
        android:id="@+id/text1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@android:color/holo_red_light" 
        android:text="text1:顶部水平居中" 
        android:textColor="@android:color/white" 
        app:layout_constraintEnd_toEndOf="parent" 
        app:layout_constraintStart_toStartOf="parent" 
        app:layout_constraintTop_toTopOf="parent" /> 
 
    <TextView 
        android:id="@+id/text2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@android:color/holo_red_light" 
        android:text="text2:位于text1下且水平居中" 
        android:textColor="@android:color/white" 
        app:layout_constraintEnd_toEndOf="parent" 
        app:layout_constraintStart_toStartOf="parent" 
        app:layout_constraintTop_toBottomOf="@id/text1" /> 
 
    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@android:color/holo_green_light" 
        android:text="水平居中,且居于text2下" 
        android:textColor="@android:color/white" 
        app:layout_constraintEnd_toEndOf="parent" 
        app:layout_constraintStart_toStartOf="parent" 
        app:layout_constraintTop_toBottomOf="@id/text2" /> 
 
 
    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@android:color/holo_red_light" 
        android:text="居中位于底部" 
        android:textColor="@android:color/white" 
        app:layout_constraintBottom_toBottomOf="parent" 
        app:layout_constraintEnd_toEndOf="parent" 
        app:layout_constraintStart_toStartOf="parent" /> 
 
 
    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@android:color/holo_blue_bright" 
        android:text="全局居中" 
        android:textColor="@android:color/white" 
        app:layout_constraintBottom_toBottomOf="parent" 
        app:layout_constraintEnd_toEndOf="parent" 
        app:layout_constraintStart_toStartOf="parent" 
        app:layout_constraintTop_toTopOf="parent" /> 
</android.support.constraint.ConstraintLayout>

输出结果如图:


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!