Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在不嵌套布局的情况下为ConstraintLayout中的某些视图制作填充背景?_Android_Android Constraintlayout - Fatal编程技术网

Android 如何在不嵌套布局的情况下为ConstraintLayout中的某些视图制作填充背景?

Android 如何在不嵌套布局的情况下为ConstraintLayout中的某些视图制作填充背景?,android,android-constraintlayout,Android,Android Constraintlayout,我在一个约束列表中有一组视图。我希望这些视图具有灰色背景。我还希望背景在这些视图周围有填充 如何在没有嵌套布局的情况下执行此操作 以下是我的XML: <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="h

我在一个约束列表中有一组视图。我希望这些视图具有灰色背景。我还希望背景在这些视图周围有填充

如何在没有嵌套布局的情况下执行此操作

以下是我的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="match_parent"
    android:paddingBottom="10dp">


    <View
        android:id="@+id/topBg"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#dddddd"
        app:layout_constraintBottom_toBottomOf="@+id/userImage"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <ImageView
        android:id="@+id/userImage"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/avatars" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:singleLine="true"
        android:textColor="#655643"
        android:textSize="15sp"
        android:textStyle="bold"
        app:layout_constraintStart_toEndOf="@+id/userImage"
        app:layout_constraintTop_toTopOf="@+id/userImage"
        tools:text="Dream Destinations" />

    <TextView
        android:id="@+id/subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:singleLine="true"
        android:textColor="#999999"
        android:textSize="13sp"
        app:layout_constraintStart_toStartOf="@+id/title"
        app:layout_constraintTop_toBottomOf="@+id/title"
        tools:text="by Someone, 921 discoveries" />
</androidx.constraintlayout.widget.ConstraintLayout>

你必须给所有的约束适当。还有根视图(Constraintlayout)的高度不需要匹配父对象,然后将其更改为换行内容



如果您想要根视图(androidx.constraintlayout.widget.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="wrap_content">


    <View
        android:id="@+id/topBg"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#dddddd"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/userImage"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/avatars" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:singleLine="true"
        android:textColor="#655643"
        android:textSize="15sp"
        android:textStyle="bold"
        app:layout_constraintStart_toEndOf="@+id/userImage"
        app:layout_constraintTop_toTopOf="@+id/userImage"
        tools:text="Dream Destinations" />

    <TextView
        android:id="@+id/subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:singleLine="true"
        android:textColor="#999999"
        android:textSize="13sp"
        app:layout_constraintStart_toStartOf="@+id/title"
        app:layout_constraintTop_toBottomOf="@+id/title"
        tools:text="by Someone, 921 discoveries" />
</androidx.constraintlayout.widget.ConstraintLayout>