其他分享
首页 > 其他分享> > 使用约束布局ConstraintLayout完美解决多控件被挤出屏幕的问题

使用约束布局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"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:background="@color/colorPrimaryDark">

    <ImageView
        android:id="@+id/icon_music"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@mipmap/music"
        android:layout_marginLeft="10dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="All I Want for Christmas Is You"
        android:textSize="28sp"
        android:maxLines="1"
        android:ellipsize="end"
        android:textColor="#ffffff"
        android:layout_marginLeft="10dp"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toStartOf="@id/img_vip"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="@id/icon_music"
        app:layout_constraintStart_toEndOf="@id/icon_music"/>

    <ImageView
        android:id="@+id/img_vip"
        android:layout_width="52dp"
        android:layout_height="52dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:src="@mipmap/icon_vip_tag"
        app:layout_constraintStart_toEndOf="@id/tv_name"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        app:layout_constraintTop_toTopOf="@id/tv_name"
        app:layout_constraintBottom_toBottomOf="@id/tv_name"/>


    <TextView
        android:id="@+id/tv_author"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mariah Carey"
        android:textColor="@color/text_accent"
        android:textSize="24sp"
        android:maxLines="1"
        android:ellipsize="end"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        app:layout_constraintStart_toStartOf="@id/tv_name"
        app:layout_constraintTop_toBottomOf="@id/tv_name" />

    <ImageView
        android:id="@+id/img_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_playing"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="left"
        app:constraint_referenced_ids="img_play"/>

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:background="#805a5a5a"
        android:layout_marginTop="15dp"
        app:layout_constraintStart_toStartOf="@id/tv_name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>
    
</androidx.constraintlayout.widget.ConstraintLayout>

更新多细节请移步 下文 来源:

使用约束布局ConstraintLayout完美解决多控件被挤出屏幕的问题

标签:控件,完美,约束,挤出,屏幕,ConstraintLayout
来源: https://blog.csdn.net/SmileToLin/article/details/120988583