首页 文章

match_parent未按预期执行

提问于
浏览
7

我想这应该是一个相当容易回答的问题,如果你比我更了解XML布局 . 在使用 match_parent layout_height时,我似乎没有得到我想的那样 .

我有 LinearLayout 根元素 android:orientation="vertical" . 在这个LinearLayout里面我想要三个元素: - TextView - ListView - TextView

对于两个TextViews,我设置 android:layout_height="wrap_content" ,以便它们只有显示其内容所需的高度 . 问题是,我希望一个TextView位于窗体的顶部,另一个位于窗体底部,而ListView填充窗体上可用的任何空间 . 所以这是我的xml布局:

<TextView
机器人:layout_width = “match_parent”
机器人:layout_height = “WRAP_CONTENT”
机器人:TEXTSIZE = “30sp”
android:text =“Top TextView”/>

<ListView的
android:id =“@ id / listView_Species”
机器人:layout_width = “match_parent”
android:layout_height =“match_parent”/>

<TextView的
机器人:layout_width = “match_parent”
机器人:layout_height = “WRAP_CONTENT”
机器人:TEXTSIZE = “30sp”
android:text =“Bottom TextView”/>

但它并不是我所得到的 . 我已经选择了ListView,以便突出显示它 . 注意它是如何一直延伸到表单的底部,将底部的TextView从表单中推出 .
XML Layout 1

当我将ListView的layout_height属性更改为某个固定值(如180dp)时,这就是表单的样子 . 我只是发布这个来证明底部的TextView在那里,但我仍然不知道如何将它固定到屏幕的底部,而ListView占用了剩余的空间,但在两个TextView之间 .

XML Layout 2

提前致谢 .

3 回答

  • 11

    虽然其他答案试图解决你的问题(他们实际上没有 - 他们建议你做一些看起来相似但在不同设备上可能会或可能看起来不太好的东西),没有人填补你对LinearLayouts知识的空白和match_parent . 这些差距非常普遍 - 谷歌的文档仍然远远低于恒星 .

    首先,Views如何在LinearLayout中工作?让我们完成绘制LinearLayout的过程,为简单起见使用 orientation="vertical" .

    • 检查LinearLayout(简称LL)的第一个子节点的高度 . 如果高度为 match_parentfill_parent (同一事物的旧名称),则会拉伸视图的高度以填充整个查看区域 . 如果高度为 wrap_content ,则测量View所采用的垂直空间,并将该空间用于View . 如果高度是非零数字,请使用视图高度的那么多像素(如果太小则可能会剪辑) . 如果高度为0,请参见下文 .

    • 将下一个视图放在视图下方1.检查其高度并采取相应措施 .

    • 继续查看所有视图 . 如果View被推离底部,请继续并停止计算,因为没有人会看到它或任何后续视图(假设没有ScrollView) .

    • 如果视图的高度为0,请检查它的 gravity . 这需要第二次传递,存储所有视图的重力,然后按比例分配它们的高度 . 正如您所猜测的那样,第二遍可以将布局所需的时间加倍,这对于简单布局来说并不重要 .

    Explanation of your example :LL的第一个子节点(第一个TextView)被测量并占用一定量的像素 . 然后您的ListView占用所有剩余空间(通过 match_parent ) . 然后你的第二个TextView根本不会被绘制,因为它不在屏幕的底部 . 这几乎是你观察到的,但现在你明白了为什么 .

    Solution :使用 RelativeLayout . 在这种情况下完美地工作 .

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/top_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:textSize="30sp"
            android:text="Top TextView" />
    
        <TextView
            android:id="@+id/bottom_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:textSize="30sp"
            android:text="Bottom TextView" />
    
        <ListView
            android:id="@+id/listView_Species"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/top_tv"
            android:layout_above="@id/bottom_tv"
            />
    </RelativeLayout>
    

    RelativeLayout告诉布局inflater在顶部绘制第一个TextView,然后在底部绘制第二个TextView,然后用ListView填充剩余的空间 . 我相信这正是你想要的 .

    欢迎使用Android . 你将使用这种模式很多!

  • 3

    ListView 高度更改为0dp并添加 weight=1
    即:

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="Top TextView" />
    
    <ListView
        android:id="@+id/listView_Species"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="Bottom TextView" />
    
  • 10

    使用android:layout_weight为最外层布局中的小部件定义权重 . 将其高度声明为 0dp ,然后为每个高度定义 android:layout_weight . 它们中的三个的总权重应该是1.根据您的需要,您可以将 0.1 权重定义到顶部和底部TextView,并将 0.8 定义为ListView .

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight = "0.1"
        android:textSize="30sp"
        android:text="Top TextView" />
    
    <ListView
        android:id="@+id/listView_Species"
        android:layout_width="match_parent"
        android:layout_weight = "0.8"
        android:layout_height="0dp" />
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:textSize="30sp"
        android:layout_weight = "0.1"
        android:text="Bottom TextView" />
    

相关问题