android - 将 TextView 放在父 ConstraintLayout 中居中,但根据左/右锚定 View 调整大小

标签 android android-layout android-constraintlayout

我需要一个单行 TextView 位于其父 ConstraintLayout 的中心,但不能与同一布局内的其他 View 重叠,以便它广告 ...末尾省略号

ConstraintLayout(高度:48dp,宽度:match_parent)包含以下3个 subview :

  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 锚定到侧面容器,文本将不会重叠,但如果容器具有不同的宽度,文本可能不会居中。

最佳答案

尝试我的解决方案:

<?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,因为如果我们约束边,我们可以使用它来拉伸(stretch) View 。

然后我将 android:gravity="center" 添加到您的 TextView 中。我还添加了 app:layout_constraintHorizo​​ntal_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

关于android - 将 TextView 放在父 ConstraintLayout 中居中,但根据左/右锚定 View 调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61682868/

相关文章:

php - Android 将 Base64 字符串发布到 PHP

android - 修改行间距

android - 向布局添加高程

android - ConstraintLayout 行为从 1.1.2 到 1.1.3 的奇怪变化

android - 我的布局适用于一种屏幕尺寸,如何使其适用于 Android Studio 3.4.2 中的所有屏幕尺寸

android - 约束布局 - 指南以编程方式设置方向

android - 在 Android gradle 构建中,在构建期间更改 strings.xml 的内容

android - 如何计算无需滚动即可适合 WebView 的文本

android - Unity 3D 插件的 Google Drive 授权错误

android - 与字符串匹配并在android中使用本地数据库检索所有可用字符串