【问题标题】:Android TextView align text vertically within the bounding boxAndroid TextView 在边界框内垂直对齐文本
【发布时间】:2019-03-15 06:46:48
【问题描述】:

注意:我在边界框内对齐文本而不是在父视图内。

当我将background color 应用到TextView 时会发生这种情况,因此我可以减少一个父布局以添加背景颜色

XML 文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/item_card"
    style="@style/CardViewTheme"
    android:layout_width="96dp"
    android:layout_height="96dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/item_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="8dp"
            android:src="@drawable/ic_item_icon"
            app:layout_constraintBottom_toTopOf="@+id/item_name"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_chainStyle="spread_inside" />

        <TextView
            android:id="@+id/item_name"
            style="@style/BodyMenuText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:gravity="center"
            android:text="Flat Tire"
            android:textAlignment="center"
            app:layout_constraintBottom_toTopOf="@+id/item_price"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/item_icon" />

        <TextView
            android:id="@+id/item_price"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorDarkestGray"
            android:gravity="center"
            android:padding="4dp"
            android:text="USD 40"
            android:textSize="@dimen/app_caption_text"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/item_name" />

    </android.support.constraint.ConstraintLayout>

</android.support.v7.widget.CardView>

【问题讨论】:

  • 如果您的目标是使父布局的文本居中,请使用 android:layout_gravity="center_horizo​​ntal"。
  • @HariRam 你准备好我的Note 报价了吗?如果没有,请阅读。
  • 我们需要整个 XML 文档。
  • @CodeLover Box 只是背景色?或者您对此有其他看法?
  • @GavinWright 我已经用完整的 XML 代码更新了问题。@HariRam 是的,我已经将背景颜色应用于 TextView 本身,所以我不必添加一个父布局来设置背景颜色.

标签: android android-layout textview android-xml


【解决方案1】:

这是您的解决方案:

<android.support.v7.widget.CardView 
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/item_card"
      android:layout_width="96dp"
      android:layout_height="wrap_content"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent">

CardView 的 layout_height 不够高,TextView 的底部被切断。将其设置为 wrap_content 即可。

【讨论】:

  • 对不起,我不知道你更新你的答案,
  • +1 - 只有一个问题.. 为什么固定大小会影响“TextView”?我必须在这里设置固定大小吗?
  • 您可以只增加固定尺寸,例如从 96dp 到 110dp。但 wrap_content 似乎是更好的解决方案。
  • 啊!我太愚蠢了:P。我明白你的意思..当高度固定时,它正在裁剪底部..明白了..非常感谢..