Android的布局剩余高度的问题

在apkbus上看到的一个问题,之前也遇到过,后来便不了了之。

请输入图片描述

下面是该问题当时的答案,总觉的有些不太优美。大家有什么好方法么?谢谢。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffe0e8ec"
    android:orientation="vertical" >
    
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="20dip" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1.0" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="20dip" />

</LinearLayout>

附原地址:
http://www.apkbus.com/android-43921-1...

---------------------------------更新-------------------------------------

由于我对android:layout_weight属性的不了解导致了这个问题的出现,经过研究发现android:layout_weight是个很有趣的属性,上面的解决方法非常巧妙。

先来看看什么是android:layout_weight:

Layout weight is used to determine how Android divides up any left over space after the various widgets get all of the space they want.

上面代码中将中间LinearLayout的android:layout_weight设置为1,也就等于中间的LinearLayout占据了剩下的所有空间,很好了解决的layout中高度的问题。

阅读 16.6k
3 个回答

思路1:用RelativeLayout布局
思路2:设置LinearLayout的android:layout_gravity
暂时想到这两个方法,不过目前没在开发环境下,明天试试看是否可行 : )

使用layout_weight

对的,用权重的话是很容易解决大部分问题的,而且对于不同分辨率的适应性还是比较不错的

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题