按钮断开LinearLayout layout_gravity顶部

时间:2012-11-13 18:47:06

标签: android android-layout android-linearlayout layout-gravity

我可能是个白痴,但似乎在水平Button休息时间LinearLayout添加了layout_gravity="top"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/text"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="top"
        android:text="TextView"
        android:background="#f00" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:layout_weight="0"
        android:text="Button" />

</LinearLayout>

这看起来像这样(在布局编辑器中):

screenshot

请注意,文字视图位于中心,而不是顶部!如果*我所做的就是删除<button>,那么它看起来像这样:

working screenshot

看,现在它应该是应该的顶部。我将使用的解决方法是让layout_gravity="start"确实导致正确的行为:

enter image description here

但无论如何,发生了什么?另请参阅this related question。那可能实际上是同一个问题;我不确定。反正没有真正的答案。

1 个答案:

答案 0 :(得分:4)

啊哈,我找到了答案!您必须设置android:baselineAligned="false"。以前从来没有听说过这种情况,并且默认情况下它很烦人!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal">

...
相关问题