在父ConstraintLayout中居中,但基于左/右锚定视图调整大小

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

我需要在其父ConstraintLayout的内部居中放置一个单行TextView,但不要在同一布局内重叠其他视图,以便在其末尾显示...省略号

ConstraintLayout(高度:48dp,宽度:match_parent),其中包含以下3个子视图:

  1. 左锚定容器(高度:48dp,宽度:变量-下图中的绿色)
  2. TextView(以ConstraintLayout居中)
  3. 右锚定容器(高度:48dp,宽度:可变-下图中的蓝色)

左/右容器的宽度不相等(一个可以为0,而另一个48dp,或者一个48dp和另一个96dp)

enter image description here

XML布局看起来像这样:

<androidx.constraintlayout.widget.ConstraintLayout 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="48dp"
    android:background="#FF0000">

    <LinearLayout
        android:id="@+id/container_start"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#00FF00"
        android:orientation="horizontal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

    <TextView
        android:id="@+id/text_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toEndOf="@id/container_start"
        app:layout_constraintEnd_toStartOf="@id/container_end"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:maxLines="1"
        android:ellipsize="end"
        android:text="Title Title Title Title" />

    <LinearLayout
        android:id="@+id/container_end"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#0000FF"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • 如果我将TextView锚定到父级(左/右),它将居中,但文本将与侧面容器重叠
  • 如果我将TextView锚定到侧面容器,则文本不会重叠,但是如果容器具有不同的宽度,则文本可能不会居中。
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:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="#FF0000">

    <LinearLayout
        android:id="@+id/container_start"
        android:layout_width="48dp"
        android:layout_height="0dp"
        android:background="#00FF00"
        android:orientation="horizontal"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/text_title" />

    <TextView
        android:id="@+id/text_title"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        app:layout_constraintStart_toEndOf="@id/container_start"
        app:layout_constraintEnd_toStartOf="@id/container_end"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:maxLines="1"
        android:ellipsize="end"
        android:text="Title Title Title Title" />

    <LinearLayout
        android:id="@+id/container_end"
        android:layout_width="96dp"
        android:layout_height="0dp"
        android:background="#0000FF"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@id/text_title"/>

</androidx.constraintlayout.widget.ConstraintLayout>

我将宽度和高度更改为0dp,因为如果我们约束侧面,则可以使用它来拉伸视图。

然后我将android:gravity="center"添加到您的TextView。而且我还要添加app:layout_constraintHorizontal_chainStyle="spread_inside"

如果有任何疑问,请在评论中讨论

更新1

如果您想在应用程序中实现工具栏,建议您使用androidx.appcompat.widget.Toolbar

<?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:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="#FF0000">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar_top"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:navigationIcon="@drawable/ic_arrow_back"
        app:menu="@menu/toolbar_actions">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:text="Toolbar Title TitleTitleTitleTitleTitle"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:ellipsize="end"
            android:textStyle="bold"
            android:layout_gravity="center" />

    </androidx.appcompat.widget.Toolbar>

</androidx.constraintlayout.widget.ConstraintLayout>

和菜单XML标记

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

    <item android:id="action_one"
        android:icon="@drawable/ic_android"
        android:title="action 1"
        app:showAsAction="ifRoom"/>

    <item android:id="action_two"
        android:icon="@drawable/ic_android"
        android:title="action 2"
        app:showAsAction="ifRoom"/>

</menu>

结果:

enter image description here

更新2

尝试这个,也许这就是您想要的。

<?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:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="#FF0000">

    <TextView
        android:id="@+id/text_title"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:gravity="center"
        android:maxLines="1"
        android:ellipsize="end"
        android:text="Title Title Title Titlsase"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="@id/center_line"
        app:layout_constraintEnd_toEndOf="@id/center_line"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/center_line"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintGuide_percent="0.5"
        android:orientation="vertical"/>

    <LinearLayout
        android:id="@+id/container_start"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#00FF00"
        android:orientation="horizontal"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/text_title" >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_arrow_back"
            android:layout_marginStart="10dp"
            android:layout_gravity="start|center_vertical" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/container_end"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#0000FF"
        android:orientation="horizontal"
        android:gravity="end"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/text_title">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_android"
            android:layout_marginEnd="10dp"
            android:layout_gravity="end|center_vertical" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_android"
            android:layout_marginEnd="10dp"
            android:layout_gravity="end|center_vertical" /> 

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_android"
            android:layout_marginEnd="10dp"
            android:layout_gravity="end|center_vertical" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

结果:

enter image description here

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