【问题标题】:Button breaks LinearLayout layout_gravity top按钮中断 LinearLayout layout_gravity top
【发布时间】:2012-11-14 02:58:09
【问题描述】:

我可能是个白痴,但似乎将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>

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

请注意,文本视图位于中心,而不是顶部!如果*我所做的只是删除&lt;button&gt;,那么它看起来像这样:

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

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

【问题讨论】:

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


    【解决方案1】:

    啊哈,我找到了答案!你必须设置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">
    
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      相关资源
      最近更新 更多