美文网首页
Android ConstraintLayout

Android ConstraintLayout

作者: 潜心之力 | 来源:发表于2018-06-11 16:42 被阅读0次

一、ConstraintLayout是一种约束布局,相当于增强版的RelativeLayout
常用属性介绍,设置的值以parent为例:
app:layout_constraintLeft_toLeftOf:控件的左边与parent左对齐
app:layout_constraintLeft_toRightOf:控件的左边在parent的右边
app:layout_constraintTop_toTopOf:控件的顶部与parent顶部对齐
app:layout_constraintTop_toBottomOf:控件的顶部在parent的底部下
app:layout_constraintRight_toLeftOf:控件的右边在parent的左边
app:layout_constraintRight_toRightOf:控件的右边与parent右边对齐
app:layout_constraintBottom_toTopOf:控件的底部在parent的顶部
app:layout_constraintBottom_toBottomOf:控件的底部与parent底部对齐
二、控件填充父布局(match_parent)

        <TextView
            android:layout_width="0dp" ->不同的地方
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/dimen_dp_06" ->只有设置了左右对齐的属性后才生效
            android:text="@string/main_home"
            android:textColor="@color/main_bottom"
            android:textSize="@dimen/dimen_sp_10"
            app:layout_constraintLeft_toLeftOf="parent"  ->与父布局左对齐
            app:layout_constraintRight_toRightOf="parent" ->与父布局右对齐
             />

三、平分父布局位置(layout_weight),以三等分为例,通过自定义属性约束控件的位置

    <LinearLayout
        android:id="@+id/main_home"
        android:layout_width="0dp"
        android:layout_height="@dimen/dimen_dp_49"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/main_follow"/>

    <LinearLayout
        android:id="@+id/main_follow"
        android:layout_width="0dp"
        android:layout_height="@dimen/dimen_dp_49"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/main_home"
        app:layout_constraintRight_toLeftOf="@id/main_mine"/>

    <LinearLayout
        android:id="@+id/main_mine"
        android:layout_width="0dp"
        android:layout_height="@dimen/dimen_dp_49"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/main_follow"
        app:layout_constraintRight_toRightOf="parent"/>

也可以使用布局属性设置等分

app:layout_constraintHorizontal_weight

四、Guideline的使用,可以精确定位控件的位置

<android.support.constraint.Guideline
        android:id="@+id/guideline_w_1"
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.15" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline_h_1"
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.15" />

    <ImageView
        android:id="@+id/info_bound"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/info_bound"
        app:layout_constraintLeft_toRightOf="@id/guideline_w_1"
        app:layout_constraintTop_toBottomOf="@id/guideline_h_1" />

相关文章

网友评论

      本文标题:Android ConstraintLayout

      本文链接:https://www.haomeiwen.com/subject/myyheftx.html