您的位置:首页 > 移动开发 > Android开发

Android小技巧——LinearLayout巧妙的平均分配空间

2017-06-22 18:36 295 查看
当我们编写Android UI的时候,肯定会遇到这样的UI设计,在屏幕宽度里面线性横向排列有三个View,每个View平分屏幕宽度。乍一看,这个很简单嘛,给这三个View都设置一个相同的width就好嘛,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="@android:color/white"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="AAAAAAAA"
android:layout_width="100dp"
android:layout_height="wrap_content"/>

<Button
android:text="BBBBBBBB"
android:layout_width="100dp"
android:layout_height="wrap_content"/>

<Button
android:text="CCCC"
android:layout_width="100dp"
android:layout_height="wrap_content"/>
</LinearLayout>


但问题是它们的宽度是不确定的,有可能动态的变化,比如说它的父布局可能不是屏幕宽度等等。所以这个方法是下下策。也许有人会说,这个也不难吗,我在java代码里面动态的去设置它们的宽度相同就好了。但这个的前提是需要先获取它的父布局里的宽度,而且当当activity生命周期执行到onresume的时候,整个当前页面UI其实还没有真正的绘制到window中,这也是我们经常在生命周期中获取到某个view的width、height为0的原因,考虑到这个,开发者往往还要借助View.getViewTreeObserver().addOnGlobalLayoutListener的布局监听来解决,既然add了,那就有remove操作等,然而回到我们的问题,我不过是要view平均分配空间而已,真的需要添加如此多的代码?

另外有一些人会说,我使用LiearLayout,设置三个view的layout_weight属性都相同不就可以了吗?例如下面代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="@android:color/white"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_weight="1"
android:text="aaa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:layout_weight="1"
android:text="bbb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:layout_weight="1"
android:text="ccc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>




看上去,上面的方法好像可以解决问题,然而如果每个子view的内容宽度不一样的时候也能保持平均分配吗?答案当然是NO!例如下面代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="@android:color/white"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_weight="1"
android:text="aaaaaaaaaaaaaaa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:layout_weight="1"
android:text="bbbbb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:layout_weight="1"
android:text="c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>




所以通过设置相同的layout_weight只能解决每个子view内容宽度都相同的情况下的问题。

现在说一个非常实用的小技巧:

在LinearLayout布局中,巧妙的设置width/height和layout_weight属性就能达到子view平均分配父view空间的效果

例子代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="@android:color/white"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_weight="1"
android:text="aaaaaaaaaaaa"
android:layout_width="0dp"
android:layout_height="wrap_content"/>

<Button
android:layout_weight="1"
android:text="bbbbb"
android:layout_width="0dp"
android:layout_height="wrap_content"/>

<Button
android:layout_weight="1"
android:text="c"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</LinearLayout>




必须要注意的是:务必将width和layout_weight搭配使用,否则代码会报错

好了,上面说的是平分父view width的例子,平分父view height的方式也是一样的,此处就不赘述了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: