【问题标题】:Android - Align different font-sizes on same baselineAndroid - 在同一基线上对齐不同的字体大小
【发布时间】:2019-01-28 21:23:38
【问题描述】:

我有 2 个相邻的 TextViews,每个都有不同的字符串,具有不同的字体大小。我希望文本在每个TextView 中具有相同的基线。我该怎么做?

这是我的布局:

   <LinearLayout
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:background="@color/colorAccentLight"
            android:textSize="18sp"
            style="@style/Base.TextAppearance.AppCompat.Medium"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="bottom"
            android:textAlignment="gravity"
            android:text="30"
        />
        <TextView
            android:background="@color/colorAccent"
            android:text="hello"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="bottom"
            android:textAlignment="gravity"
        />

    </LinearLayout>

这是当前行为,请注意 2 个TextViews 有不同的基线(“hello”低于 30),因为每个 TextView 的字体大小不同。

【问题讨论】:

  • 还不能写 cmets。你看here了吗?
  • 谢谢。我最终使用了这个解决方案(将 TextViews 包装在 RelativeLayout 中并使用“align_baseline”属性。

标签: android android-layout textview android-xml


【解决方案1】:

ConstraintLayout你可以简单地使用

app:layout_constraintBaseline_toBaselineOf="@+id/textView1"

【讨论】:

    【解决方案2】:

    将两个TextView 元素的高度更改为wrap_content。默认情况下,水平的LinearLayout 会自动对齐 TextView 子项的基线。

    如果您当前使用match_parent 作为高度以获得全高背景颜色,则您必须想办法实现这一目标。您也许可以在LinearLayout 上使用背景颜色,然后只在较大的TextView 上指定背景颜色;这将产生与今天相同的效果。

    【讨论】:

      猜你喜欢
      • 2018-07-04
      • 1970-01-01
      • 2017-04-14
      • 2012-04-12
      • 2014-07-24
      • 1970-01-01
      • 1970-01-01
      • 2013-07-20
      • 1970-01-01
      相关资源
      最近更新 更多