Android 将两个项目约束到线性布局内的顶部和底部

Android 将两个项目约束到线性布局内的顶部和底部,android,xml,android-linearlayout,Android,Xml,Android Linearlayout,我有一个约束布局,里面有一个图像和一个线性布局。线性布局包含两个文本视图,我希望它们显示在线性布局约束的顶部和底部: <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/chatLayout" xmlns:android="http://schemas.android.com/apk/res/andro

我有一个约束布局,里面有一个图像和一个线性布局。线性布局包含两个文本视图,我希望它们显示在线性布局约束的顶部和底部:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/chatLayout"
    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="wrap_content">

    <ImageView
        android:id="@+id/chatPictureIV"
        android:layout_width="@dimen/chat_image_size"
        android:layout_height="@dimen/chat_image_size"
        android:scaleType="centerInside"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="@dimen/chat_image_size"
        android:orientation="vertical"
        app:layout_constraintStart_toEndOf="@+id/chatPictureIV"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/chatNameTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Top"
            android:layout_gravity="top"
            android:textStyle="bold"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/chatStatusTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:text="Bottom"
            android:textSize="18sp" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>


如何将底部文本约束到线性布局的底部?

在这种情况下,
RelativeLayout
更合适。
您所要做的就是将两个视图的属性
android:layout\u alignParentTop
android:layout\u alignParentBottom
设置为
true

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/chatLayout"
    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="wrap_content">

    <ImageView
        android:id="@+id/chatPictureIV"
        android:layout_width="@dimen/chat_image_size"
        android:layout_height="@dimen/chat_image_size"
        android:scaleType="centerInside"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="@dimen/chat_image_size"
        app:layout_constraintStart_toEndOf="@+id/chatPictureIV"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/chatNameTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Top"
            android:layout_alignParentTop="true"
            android:textStyle="bold"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/chatStatusTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="Bottom"
            android:textSize="18sp" />

    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

在这种情况下,
相对值更合适。
您所要做的就是将两个视图的属性
android:layout\u alignParentTop
android:layout\u alignParentBottom
设置为
true

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/chatLayout"
    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="wrap_content">

    <ImageView
        android:id="@+id/chatPictureIV"
        android:layout_width="@dimen/chat_image_size"
        android:layout_height="@dimen/chat_image_size"
        android:scaleType="centerInside"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="@dimen/chat_image_size"
        app:layout_constraintStart_toEndOf="@+id/chatPictureIV"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/chatNameTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Top"
            android:layout_alignParentTop="true"
            android:textStyle="bold"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/chatStatusTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="Bottom"
            android:textSize="18sp" />

    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

试试这种方法

<TextView
            android:id="@+id/chatNameTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Top"
            android:gravity="top"
            android:layout_gravity="top"
            android:textStyle="bold"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/chatStatusTV"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="bottom"
            android:text="Bottom"
            android:gravity="bottom"
            android:textSize="18sp" />

试试这种方法

<TextView
            android:id="@+id/chatNameTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Top"
            android:gravity="top"
            android:layout_gravity="top"
            android:textStyle="bold"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/chatStatusTV"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="bottom"
            android:text="Bottom"
            android:gravity="bottom"
            android:textSize="18sp" />

您可以尝试使用嵌套的LinearLayout,如下所示:

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="200dp"
android:orientation="vertical"
app:layout_constraintStart_toEndOf="@+id/chatPictureIV"
app:layout_constraintTop_toTopOf="parent">

<TextView
    android:id="@+id/chatNameTV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Top"
    android:layout_gravity="top"
    android:textStyle="bold"
    android:textSize="18sp" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="bottom"
    android:orientation="vertical">

    <TextView
        android:id="@+id/chatStatusTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Bottom"
        android:textSize="18sp" />
</LinearLayout>


您可以尝试使用嵌套的LinearLayout,如下所示:

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="200dp"
android:orientation="vertical"
app:layout_constraintStart_toEndOf="@+id/chatPictureIV"
app:layout_constraintTop_toTopOf="parent">

<TextView
    android:id="@+id/chatNameTV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Top"
    android:layout_gravity="top"
    android:textStyle="bold"
    android:textSize="18sp" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="bottom"
    android:orientation="vertical">

    <TextView
        android:id="@+id/chatStatusTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Bottom"
        android:textSize="18sp" />
</LinearLayout>


您不需要使用任何嵌套的视图组。基于此,最好在平面层次结构中通过根约束来处理它

<?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:id="@+id/chatLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/chatPictureIV"
        android:layout_width="@dimen/chat_image_size"
        android:layout_height="@dimen/chat_image_size"
        android:scaleType="centerInside"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/chatNameTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Top"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintStart_toEndOf="@+id/chatPictureIV"
        app:layout_constraintTop_toTopOf="@id/chatPictureIV" />

    <TextView
        android:id="@+id/chatStatusTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Bottom"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="@id/chatPictureIV"
        app:layout_constraintStart_toEndOf="@+id/chatPictureIV" />


</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"
    android:id="@+id/chatLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/chatPictureIV"
        android:layout_width="@dimen/chat_image_size"
        android:layout_height="@dimen/chat_image_size"
        android:scaleType="centerInside"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/chatNameTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Top"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintStart_toEndOf="@+id/chatPictureIV"
        app:layout_constraintTop_toTopOf="@id/chatPictureIV" />

    <TextView
        android:id="@+id/chatStatusTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Bottom"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="@id/chatPictureIV"
        app:layout_constraintStart_toEndOf="@+id/chatPictureIV" />


</androidx.constraintlayout.widget.ConstraintLayout>


在这种情况下,不需要使用RelativeLayout或任何其他视图组。基于页面,在这种情况下最好删除RelativeLayout。@MirMiladHosseiny在这种情况下最好删除RelativeLayout这是您的解决方案吗?如果有,请发布。请检查我的。在这种情况下,您不需要使用RelativeLayout或任何其他视图组。基于页面,在这种情况下最好删除RelativeLayout。@MirMiladHosseiny在这种情况下最好删除RelativeLayout这是您的解决方案吗?如果你有一个,然后张贴。请检查我的。