新Layout界面局部配置基本(androidX 库,Constraintlayout)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <TextView //布置一个Text区域
        android:id="@+id/tv1"//名称声明
        android:layout_width="200dp"//设置宽度
        android:layout_height="150dp"
        android:background="#f5ec7e"设置底色

        android:text="TV1"//设置text文字内容(建议@引用字符)
        android:gravity="center_vertical"//文字位置(将会靠框左侧垂直居中)

<!--等价于将四边锚定父级四边,并持有一个力,如果只有Bottom锚定,没有Top锚定,上下力失衡,该元素将会拉到父级底部-->        app:layout_constraintBottom_toBottomOf="parent"//底锚定父级框底
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
<!--约束拉力强度,水平0.7会在布局中心向右偏移-->     
        app:layout_constraintHorizontal_bias="0.7"
        app:layout_constraintVertical_bias="0.1" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="0dp"//与其说宽度为0,不如说自由未设置,这样下面锚定约束会自动调整大小
        android:layout_height="wrap_content"

        android:background="#68b0f9"
        android:gravity="center"
        android:text="TV2"

        android:layout_marginEnd="20dp"//父级与边缘保持20边距的约束

        app:layout_constraintLeft_toLeftOf="@id/tv1"// 左边锚定到tv1布局元素的左边,等同于与tv1左边对齐
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/tv1"

<!--由于设置了宽度自由,所以此设置移动元素位置无效(在保证上面约束的前提下自由伸展,所以不管怎么设置约束力大小来使元素位置偏移,元素都会始终保持这么大)-->     
        app:layout_constraintHorizontal_bias="0.7"
        app:layout_constraintVertical_bias="0.1"
        />

</androidx.constraintlayout.widget.ConstraintLayout>
tv1布局展示.png
tv2布局展示.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容