layout_above坏了吗?这种简单的布局让我发疯

时间:2015-03-02 21:56:17

标签: android android-layout relativelayout

这是我的代码

<TextView
    android:id="@+id/BIG"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="BIG"
    android:textSize="50dp" />

<TextView
    android:id="@+id/small"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@id/BIG"
    android:layout_toRightOf="@id/BIG"
    android:text="small"
    android:textSize="10dp"

    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/small"
    android:layout_alignLeft="@id/small"
    android:text="small above"
    android:textSize="10dp" />

这是我得到的结果(实际截图)。正如您所看到的,整个文本视图已经消失。

screenshot

这是我想要的结果(在mspaint上编辑)

enter image description here

由于自动填充,我无法使用align_bottom。如果我使用align_bottom而不是align_Baseline(实际屏幕截图)

,这就是它的样子

enter image description here

2 个答案:

答案 0 :(得分:2)

显然,在已经执行所有其他垂直对齐之后执行基线对齐 因此,“small above”“small”上方排列,当它仍处于默认位置时。然后,“小”与“BIG”的基线对齐,在 RelativeLayout 的顶部,在视图之外留下“小于”。

这个问题的一个潜在解决方案是将两个较小的TextView包装在LinearLayout中,然后可以与左侧较大的TextView正确地进行基线对齐。
 并且还将 android:baselineAlignedChildIndex =“1”添加到LinearLayout,以便第二个孩子的基线与“BIG”对齐

参考:http://scottweber.com/2014/02/06/working-with-baselines-in-relativelayout/

答案 1 :(得分:0)

试试这个:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
    android:id="@+id/BIG"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="BIg"
    android:textSize="50sp"
    android:layout_gravity="bottom" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="bottom">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="small above"
        android:textSize="10sp" />

    <TextView
        android:id="@+id/small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="small"
        android:textSize="10sp" />


</LinearLayout>

并使用sp而不是dp作为文本大小。