【问题标题】:Aligning baseline of two text views in Android在Android中对齐两个文本视图的基线
【发布时间】:2020-05-10 01:12:55
【问题描述】:

请在下面找到我的布局及其结果;我需要文本 'Text message' 与文本 'Header' 的基线对齐(请在代码和快照下方找到)。如果有人可以指导我会很高兴

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1.0"
android:baselineAligned="true"
>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.2"
    android:background="@color/custom_orange"
    android:gravity="center"
    >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Header"
        android:textSize="25sp"
        android:id="@+id/title"
        android:gravity="center"
        android:background="@color/custom_red"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Text message"
        android:textSize="10sp"
        android:id="@+id/language"
        android:gravity="center"
        android:background="@color/custom_green"/> 
</LinearLayout>

</LinearLayout>

这是结果

这就是我需要的

【问题讨论】:

    标签: android textview


    【解决方案1】:

    使用RelativeLayout作为TextViews的容器并添加属性

    android:layout_alignBaseline="@+id/title"
    

    id/语言 TextView

    【讨论】:

      【解决方案2】:

      只需在 LinearLayout 上使用 android:gravity="bottom"(两个 textview 的线性)和两个 TextView 中的 android:layout_height="wrap_content"。

      【讨论】:

      • 确实如此。当我无意中发现这一点时,我感到非常惊讶。现在我正在尝试理解 HOW。
      • 我知道这真的很老了,但我将把它留在这里,因为我认为它很有帮助,LinearLayouts 有一个名为“baselineAligned”的属性(默认打开),它的功能正是它的名字所暗示的:根据它们的基线(几乎每个 TextView 子类)对齐视图,除非它们具有重力,这就是 OP 的布局无法正常工作的原因。在此处查看更多信息:possiblemobile.com/2013/10/shifty-baseline-alignment
      • @JonathanHernández 感谢您留下评论和链接,这确实很有帮助!
      【解决方案3】:

      在组件树上选择您想要在同一基础中的两个组件,右键单击然后选择显示基线。只需拖动两个组件的基线。

      【讨论】:

        【解决方案4】:

        对于 ConstraintLayout,只需使用这个↓ 来对齐另一个 textView 基线:

        app:layout_constraintBaseline_toBaselineOf="@+id/textView"
        

        【讨论】:

          猜你喜欢
          • 2013-05-15
          • 2019-12-27
          • 2016-07-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多