【问题标题】:Different Gravity of two textviews in a horizontal Linear Layout水平线性布局中两个文本视图的不同重力
【发布时间】:2016-10-27 18:48:11
【问题描述】:

我正在使用以下代码,当我将第一个 textview 的重力设置为center 时,第二个 textview 的文本也会自动与第一个对齐。即使我将第二个视图的重心设置为top

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="96dp"
        android:text="New Text"
        android:id="@+id/textView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:text="New Text"
        android:id="@+id/textView2" />
</LinearLayout>

another question 中有一个解决方案,它将第二个 textview 包装在另一个 LinearLayout 中。但是为什么会这样呢?

【问题讨论】:

  • 而不是使用2线性布局,使用相对布局。只是我的 2 美分。
  • LinearLayout 意味着您可以一一对齐视图(垂直/水平)。以您初始化的方式按顺序进行。

标签: android android-layout android-gravity


【解决方案1】:

默认情况下,水平LinearLayout 将其子Views 与它们的基线对齐,因此第二个TextView 被移动以使其文本与第一个对齐。要解决您的问题,只需将LinearLayoutbaselineAligned 属性设置为false

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false">

【讨论】:

    猜你喜欢
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 2012-09-27
    相关资源
    最近更新 更多