保持 ConstraintLayout 的最小宽度

问题描述 投票:0回答:1

我需要显示这样的信息框:

但我想要:

  1. 尽可能保持整个盒子的宽度最小(但包裹实际内容)
  2. 将其放在活动的左上角
  3. 数字应向右对齐并与框右边框对齐
  4. “更多”按钮应水平居中

如何修复我的布局以满足上述条件?

这是我当前的布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/box_values"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:padding="10dp"
        android:background="@color/purple_200">

        <TextView
            android:id="@+id/title1"
            android:text="Param 1:"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
        <TextView
            android:id="@+id/value1"
            android:text="-464.44"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBaseline_toBaselineOf="@id/title1"
            app:layout_constraintStart_toEndOf="@id/barrier_titles"
            app:layout_constraintEnd_toEndOf="parent"/>
        <View
            android:id="@+id/divider1"
            android:layout_width="wrap_content"
            android:layout_height="2dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@id/title1"
            android:background="@color/teal_700"/>

        <TextView
            android:id="@+id/title2"
            android:text="P2:"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/divider1" />
        <TextView
            android:id="@+id/value2"
            android:text="2.1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBaseline_toBaselineOf="@id/title2"
            app:layout_constraintStart_toEndOf="@id/barrier_titles"
            app:layout_constraintEnd_toEndOf="parent"/>
        <View
            android:id="@+id/divider2"
            android:layout_width="wrap_content"
            android:layout_height="2dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@id/title2"
            android:background="@color/teal_700"/>

        <androidx.constraintlayout.widget.Barrier
            android:id="@+id/barrier_titles"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingHorizontal="2dp"
            app:barrierDirection="end"
            app:constraint_referenced_ids="title1,title2"/>

        <Button
            android:text="More"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:flow_horizontalAlign="center"
            app:layout_constraintTop_toBottomOf="@id/divider2"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

</FrameLayout>
android android-layout android-constraintlayout
1个回答
0
投票

不幸的是,您的设置无法实现您想要做的事情。用这个代替:

<?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/parent"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.cardview.widget.CardView
    android:id="@+id/box_values"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@color/purple_200"
    android:padding="10dp"
    app:cardBackgroundColor="@color/purple_200"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/clayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/title1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Param 1:"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/value1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-464.44"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <View
            android:id="@+id/divider1"
            android:layout_width="wrap_content"
            android:layout_height="2dp"
            android:background="@color/teal_700"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/title1" />

        <TextView
            android:id="@+id/title2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="P2:"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/divider1" />

        <TextView
            android:id="@+id/value2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2.1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/divider1" />

        <View
            android:id="@+id/divider2"
            android:layout_width="wrap_content"
            android:layout_height="2dp"
            android:background="@color/teal_700"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/title2" />

        <androidx.constraintlayout.widget.Barrier
            android:id="@+id/barrier_titles"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingHorizontal="2dp"
            app:barrierDirection="end"
            app:constraint_referenced_ids="title1,title2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="More"
            app:flow_horizontalAlign="center"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/divider2" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>`

如果您使用此布局,一切都取决于分隔线的宽度。因为您使用了它们,所以它们划分了布局,但即使您配置了

wrap_content
,它们似乎也占用了大量空间。您可以删除它们并更改您的布局,以便您可以实现您想要做的事情。

© www.soinside.com 2019 - 2024. All rights reserved.