以这样一种方式添加约束,即视图不会超出它

我在使用ConstraintLayout的视图中具有以下配置:

以这样一种方式添加约束,即视图不会超出它

我希望“管理员”视图始终位于用户名视图的右侧,但是当用户名太大时,管理员视图应始终可见。如何实现?

当前:

以这样一种方式添加约束,即视图不会超出它

编辑:

当前代码

  <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="@dimen/dp0"
        android:layout_height="match_parent"
        android:layout_weight="0.8">

        <TextView
            android:id="@+id/name"
            style="@style/Text.Black"
            android:layout_width="wrap_content"
            android:layout_marginTop="12dp"
            android:ellipsize="marquee"
            android:paddingBottom="@dimen/dp6"
            android:singleLine="true"
            android:textSize="@dimen/sp16"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="veryveryveryverylongvervrusername" />

        <TextView
            android:id="@+id/mail"
            style="@style/Text.Grey"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/name"
            android:ellipsize="marquee"
            android:singleLine="true"
            tools:text="fjfdjdf"
            android:textSize="@dimen/sp12" />

        <TextView
            android:id="@+id/status"
            style="@style/Text.White"
            android:layout_height="@dimen/dp18"
            android:layout_marginLeft="@dimen/dp9"
            android:layout_marginStart="@dimen/dp9"
            android:layout_marginTop="@dimen/dp2"
            app:layout_constraintStart_toEndOf="@id/name"
            app:layout_constraintTop_toTopOf="@+id/name"
            android:ellipsize="marquee"
            android:paddingLeft="@dimen/dp5"
            android:paddingRight="@dimen/dp5"
            android:singleLine="true"
            tools:text="Admin"
            tools:background="@drawable/bg_text_round_corners_blue"
            android:textSize="@dimen/sp12" />

ccc_crazy_2000 回答:以这样一种方式添加约束,即视图不会超出它

首先必须添加Admin Textview(@ id + id / status)

app:layout_constraintEnd_toEndOf="parent"

然后在用户名文本视图(@ + id / name)上,尝试添加以下行,

app:layout_constrainedWidth="true"
app:layout_constraintEnd_toStartOf="@id/status"

在管理员TextView之前谁应该将宽度限制为最大

更新(因为我不完全了解您的限制)

我想弄清楚这项工作的唯一方法是在已经存在的内部创建另一个ConstraintLayout,如下所示:

   <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.8"
        android:background="@color/colorPrimaryDark">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constrainedWidth="true"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"
                android:singleLine="true"
                app:layout_constrainedWidth="true"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/status"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                tools:layout_editor_absoluteY="12dp"
                tools:text="veryveryveryverylongvervrusername" />


            <TextView
                android:id="@+id/status"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"
                android:singleLine="true"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toEndOf="@+id/name"
                tools:layout_editor_absoluteY="12dp"
                tools:text="Admin" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

重点是设置

    app:layout_constrainedWidth="true"

用于用户名TextView和内部ConstraintLayout

,

您可以尝试将Admin文本视图的layout_witdh设置为0dp,如下所示:

android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@+id/name"
app:layout_constraintEnd_toStartOf="@+id/settings_icon"

textview名称需要具有以下内容:

android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="@+id/status"

,

对于ID为@+id/name的字段,在以下行添加:

app:layout_constraintEnd_toStartOf="@id/status"
android:layout_height="wrap_content"

并将其layout_width更改为

android:layout_width="0dp"
,

尝试:

 <TextView
            android:id="@+id/status"
            style="@style/Text.White"
            android:layout_height="@dimen/dp18"
            android:layout_marginLeft="@dimen/dp9"
            android:layout_marginStart="@dimen/dp9"
            android:layout_marginTop="@dimen/dp2"
            app:layout_constraintStart_toEndOf="@id/name"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:ellipsize="marquee"
            android:paddingLeft="@dimen/dp5"
            android:paddingRight="@dimen/dp5"
            android:singleLine="true"
            tools:text="Admin"
            tools:background="@drawable/bg_text_round_corners_blue"
            android:textSize="@dimen/sp12" />
,

@ bvk256

您可以使用以下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:minHeight="90dp"
android:padding="16dp"
tools:context=".MainActivity">

<ImageView
    android:id="@+id/startIcon"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:src="@android:drawable/ic_btn_speak_now"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/firstEditText"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:padding="0dp"
    app:layout_constraintEnd_toStartOf="@id/admin"
    app:layout_constraintStart_toEndOf="@+id/startIcon"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="hello world to this long and very very very very long text" />

<TextView
    android:id="@+id/admin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:background="@color/colorAccent"
    android:text="Admin"
    app:layout_constraintEnd_toStartOf="@+id/endIcon"
    app:layout_constraintTop_toTopOf="@+id/firstEditText" />

<EditText
    android:id="@+id/secondEditText"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:padding="0dp"
    app:layout_constraintEnd_toStartOf="@id/admin"
    app:layout_constraintStart_toEndOf="@+id/startIcon"
    app:layout_constraintTop_toBottomOf="@+id/firstEditText"
    tools:text="hello world to this long and very very very very long text" />

<ImageView
    android:id="@+id/endIcon"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:src="@android:drawable/ic_menu_delete"
    android:tint="@android:color/holo_blue_dark"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

enter image description here

,

谢谢大家的答复。我尝试了所有解决方案,但是当“用户名”过大时,它们要么将“管理员”文本从可见性中挤出,要么使“管理员”文本视图粘贴到右侧而没有考虑“用户名”的大小。

通过将此百分比约束添加到“用户名”文本视图中,我可以在一定程度上解决问题:

android:layout_width="0dp"
app:layout_constraintWidth_max="wrap"
app:layout_constraintWidth_percent="0.75"

因此它的行为如下: enter image description here enter image description here

本文链接:https://www.f2er.com/3101637.html

大家都在问