Android小技巧——LinearLayout巧妙的平均分配空间
2017-06-22 18:36
295 查看
当我们编写Android UI的时候,肯定会遇到这样的UI设计,在屏幕宽度里面线性横向排列有三个View,每个View平分屏幕宽度。乍一看,这个很简单嘛,给这三个View都设置一个相同的width就好嘛,如下:
但问题是它们的宽度是不确定的,有可能动态的变化,比如说它的父布局可能不是屏幕宽度等等。所以这个方法是下下策。也许有人会说,这个也不难吗,我在java代码里面动态的去设置它们的宽度相同就好了。但这个的前提是需要先获取它的父布局里的宽度,而且当当activity生命周期执行到onresume的时候,整个当前页面UI其实还没有真正的绘制到window中,这也是我们经常在生命周期中获取到某个view的width、height为0的原因,考虑到这个,开发者往往还要借助View.getViewTreeObserver().addOnGlobalLayoutListener的布局监听来解决,既然add了,那就有remove操作等,然而回到我们的问题,我不过是要view平均分配空间而已,真的需要添加如此多的代码?
另外有一些人会说,我使用LiearLayout,设置三个view的layout_weight属性都相同不就可以了吗?例如下面代码:
看上去,上面的方法好像可以解决问题,然而如果每个子view的内容宽度不一样的时候也能保持平均分配吗?答案当然是NO!例如下面代码:
所以通过设置相同的layout_weight只能解决每个子view内容宽度都相同的情况下的问题。
现在说一个非常实用的小技巧:
在LinearLayout布局中,巧妙的设置width/height和layout_weight属性就能达到子view平均分配父view空间的效果
例子代码:
必须要注意的是:务必将width和layout_weight搭配使用,否则代码会报错
好了,上面说的是平分父view width的例子,平分父view height的方式也是一样的,此处就不赘述了。
<?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的方式也是一样的,此处就不赘述了。
相关文章推荐
- Android TabLayout 在宽屏幕上tab不能平均分配的问题解决
- Android LinearLayout中元素按比例分配宽度
- Android TabLayout 在宽屏幕上tab不能平均分配的问题解决
- Android GridLayout 动态添加子控件 + 平均分配空间
- Caused by: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
- Android 布局(LinearLayout\TableLayout\RelativeLayout\AbsoluteLayout\FrameLayout)
- LinearLayout android:layout_gravity="right" 无效问题
- Android LinearLayout 自动换行
- Android UI布局之LinearLayout
- Android之LinearLayout线性布局
- Android线性布局(Linear Layout)
- [Android UI界面] 用layout_weight实现ActivityGroup子Activity下剩余空间自动填充问题
- android四种布局管理器 FrameLayout LinearLayout TableLayout RelativeLayout
- Android--布局方式(LinearLayout)学习
- android自定义view继承linearLayout,view大小大于屏幕
- Android学习—LinearLayout布局中实现左右对齐
- Android布局之LinearLayout自定义高亮背景的方法
- Android界面——LinearLayout和RelativeLayout 属性对比
- android,利用layoutParams代码动态布局空间位置
- android布局之LinearLayout, layout_weight