美文网首页
ConstraintLayout背景图片上的某个固定位置添加控件

ConstraintLayout背景图片上的某个固定位置添加控件

作者: 失足者 | 来源:发表于2019-10-14 18:08 被阅读0次

    有时候在开发中,有这样的需求。你只有一张完整的图片,但是你要在图片中的某个位置添加写控件显示内容或者按钮。如果使用传统的RelateLayout是无法做到屏幕适配的,遇到不同的分辨率屏幕,会有严重的偏差。这时候可以使用ConstraintLayout布局。

    重要属性:

    取值范围0~1.也可以理解为在水平或者垂直方向上的百分比位置
    layout_constraintHorizontal_bias   表示控件在布局水瓶偏移量
    layout_constraintVertical_bias  表示控件在布局水瓶偏移量
    

    添加依赖
    implements “com.android.support.constraint:constraint-layout:1.1.3”

    注意:使用以上两个属性,必须要在有约束条件下才能有效果。即:

          <!--使用水平或垂直偏移量属性要添加相对于的约束属性
          constraintHorizontal:对应左右
          constraintVertical:对应上下-->
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintBottom_toBottomOf="parent"
    

    上代码:

    <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <ImageView
                android:id="@+id/iv_pressure_bg"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_gravity="left|center_vertical"
                android:scaleType="fitXY"
                android:src="@drawable/pressure1"
                tools:layout_editor_absoluteY="0dp"
                tools:layout_editor_absoluteX="0dp" />
            <TextView
                android:id="@+id/tv_pressure_freezer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#7CFC00"
                android:text="70pa1"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="0.2"
                app:layout_constraintVertical_bias="0.2"/>
    
            <TextView
                android:id="@+id/tv_pressure_culture_one"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#7CFC00"
                android:text="70pa"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="0.4"
                app:layout_constraintVertical_bias="0.5"/>
            <TextView
                android:id="@+id/tv_pressure_culture_two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#7CFC00"
                android:text="70pa"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="0.59"
                app:layout_constraintVertical_bias="0.64" />
        </android.support.constraint.ConstraintLayout>
    

    效果图:


    图片上固定位置添加控件

    相关文章

      网友评论

          本文标题:ConstraintLayout背景图片上的某个固定位置添加控件

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