【问题标题】:set proper constraint in bottom android在底部 android 中设置适当的约束
【发布时间】:2022-02-03 12:56:18
【问题描述】:

嘿,我正在研究 android 中的约束布局。我正在尝试将我的文本视图和图像视图放在同一行中,并且我的图像尺寸更大。

.

问题是我想给出适当的约束。设置适当的约束后,它将如下所示。

activity.xml

<?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="wrap_content"
    android:paddingStart="10dp"
    android:paddingTop="12dp"
    tools:ignore="RtlSymmetry">

    <TextView
        android:id="@+id/testName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/description"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="blah blah blah" />

    <TextView
        android:id="@+id/description"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_marginEnd="5dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/testIcon"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/testName"
        tools:text="blah blah blah blah blah blah blah blah blah blah blah blah" />

    <ImageView
        android:id="@+id/testIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image_pills"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/description"
        app:layout_constraintTop_toBottomOf="@+id/testName"
        tools:ignore="ContentDescription" />

</androidx.constraintlayout.widget.ConstraintLayout>

有人可以指导我如何设置适当的约束。

【问题讨论】:

    标签: android kotlin android-layout android-constraintlayout constraint-layout-chains


    【解决方案1】:

    在第一个文本视图中,您需要进行以下更改:

    android:layout_height="0dp"
    app:layout_constraintEnd_toStartOf="@+id/testIcon"
    

    在第二个文本视图中,您需要进行以下更改:

    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="@+id/testIcon"
    

    在图像视图中你需要删除这一行:

    app:layout_constraintStart_toEndOf="@+id/description"
    app:layout_constraintTop_toBottomOf="@+id/testName"
    

    这是我的示例工作代码:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingHorizontal="@dimen/dimen_12"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toStartOf="@id/imageview"
        app:layout_constraintBottom_toTopOf="@id/textView1"
        android:text="blah blah blah"/>
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/textView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/imageview"
        app:layout_constraintBottom_toBottomOf="@id/imageview"
        android:text="blah blah blah blah blah blah blah blah blah blah blah blah"/>
    
    <ImageView
        android:id="@+id/imageview"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

    • 我不希望 textView 到 app:layout_constraintEnd_toStartOf 到 imageView
    • 那么只需设置约束底部属性即可
    猜你喜欢
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多