【问题标题】:What is the baseline in RelativeLayout?RelativeLayout 的基线是什么?
【发布时间】:2011-06-22 22:34:15
【问题描述】:

在相对布局的上下文中使用“基线”指的是什么?可能是简单的问题,但文档和谷歌没有提供任何提示。

【问题讨论】:

    标签: android android-relativelayout


    【解决方案1】:

    术语baseline comes from typography。它是文本中不可见的行字母。

    例如,假设您将两个 TextView 元素并排放置。你给第二个TextView 一个大的填充(比如20dp)。如果将layout_alignBaseline 添加到第二个元素,文本将“向上移动”以与第一个元素的基线对齐。两个元素中的文本看起来就像写在同一条不可见的行上一样。

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
      <TextView
          android:id="@+id/text1"
          android:text="aatlg"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />
      <TextView
          android:text="joof"
          android:background="#00ff00"
          android:padding="20dp"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_toRightOf="@id/text1"
          android:layout_alignBaseline="@id/text1"
          />
    </RelativeLayout>
    

    【讨论】:

    • 添加解释性图像会很棒
    • 默认也是如此
    【解决方案2】:

    这是一个视觉解释,可能会澄清克里斯蒂安的答案:

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <TextView
            android:id="@+id/text1"
            android:text="Lorem"
            android:background="@android:color/holo_blue_light"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:text="Ipsum"
            android:background="@android:color/holo_orange_light"
            android:padding="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/text1"
            android:layout_alignBaseline="@id/text1" />
    </RelativeLayout>
    

    此代码将如下所示:

    现在,如果我删除 android:layout_alignBaseline 属性,相同的布局看起来像这样:

    有趣的是观察到橙色视图的高度会受到影响(在第一种情况下,填充没有应用于视图顶部)。

    【讨论】:

      猜你喜欢
      • 2015-05-25
      • 2014-12-18
      • 1970-01-01
      • 2012-01-02
      • 2016-10-23
      • 2014-08-29
      • 1970-01-01
      • 2016-07-11
      • 1970-01-01
      相关资源
      最近更新 更多