Dew*_*oel 8 android android-linearlayout android-xml xml-layout
我想这应该是一个相当容易回答的问题,如果你比我更了解XML布局.在使用match_parent layout_height 时,我似乎没有得到我想的.
我有一个带有android:orientation ="vertical"的LinearLayout根元素.在这个LinearLayout里面我想要三个元素: - TextView - ListView - TextView
对于TextViews,我设置了android:layout_height ="wrap_content",这样它们只会显示其显示内容所需的高度.问题是,我希望一个TextView位于窗体的顶部,另一个位于窗体底部,而ListView填充窗体上可用的任何空间.所以这是我的xml布局:
Run Code Online (Sandbox Code Playgroud)<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="match_parent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp" android:text="Bottom TextView" />
但它不起作用.这就是我得到的.我已经选择了ListView,以便突出显示它.注意它是如何一直延伸到表单的底部,将底部的TextView从表单中推出.

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

提前致谢.
Sco*_*ggs 16
虽然其他答案试图解决你的问题(他们实际上没有 - 他们建议你做一些看起来相似但在不同设备上可能会或可能看起来不太好的东西),没有人填补你对LinearLayouts知识的空白和match_parent.这些差距非常普遍 - 谷歌的文档仍然远远低于恒星.
首先,Views如何在LinearLayout中工作?orientation="vertical"为简单起见,我们将完成绘制LinearLayout的过程.
match_parent或fill_parent(同一事物的旧名称),则视图的高度将被拉伸以填充整个查看区域.如果高度为wrap_content,则测量View所采用的垂直空间,并将该空间用于View.如果高度是非零数字,请使用视图高度的那么多像素(如果太小则可能会剪辑).如果高度为0,请参见下文.gravity.这需要第二次传递,存储所有视图的重力,然后按比例分配它们的高度.正如您所猜测的那样,第二遍可以将布局所需的时间加倍,这对于简单布局来说并不重要.示例说明:LL的第一个子节点(第一个TextView)被测量并占用一定量的像素.然后,ListView将占用所有剩余空间(通过match_parent).然后你的第二个TextView根本不会被绘制,因为它不在屏幕的底部.这几乎是你观察到的,但现在你明白了为什么.
解决方案:使用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>
Run Code Online (Sandbox Code Playgroud)
RelativeLayout告诉布局inflater在顶部绘制第一个TextView,然后在底部绘制第二个TextView,然后用ListView填充剩余的空间.我相信这正是你想要的.
欢迎使用Android.你将使用这种模式很多!
小智 11
将ListView高度更改为0dp并添加weight=1
ie:
<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" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13394 次 |
| 最近记录: |