使用weight属性实现视图的居中显示
有的时候我们开发一个产品的时候需要让其中某个控件的宽度或高度占据其父容器的宽度或高度的一半显示,这个时候由于设备尺寸的限制,做到在每个设备上都具有同样的效果的话,我们就需要用到weightSum属性和layout_weight属性。
具体的实现过程分析如下:
首先为父容器指定一个weightSum,然后为其子控件的layout_weight属性值设为weightSum的一半,这个时候,就实现了占据其一半的效果。
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="fill_parent" 3 android:layout_height="fill_parent" 4 android:background="#ffffff" 5 android:gravity="center" 6 android:orientation="horizontal" 7 android:weightSum="1" > 8 9 <Button 10 android:layout_width="0dp" 11 android:layout_height="wrap_content" 12 android:layout_weight="0.5" 13 android:text="@string/activity_main_click_me" /> 14 15 </LinearLayout>
作者:无言
如果您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】
如果您希望与我交流互动,欢迎微博互粉
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。