在LinearLayout中,巧妙使用weightSum和layout_weight

在LinearLayout的XML中,举个例子:android:weightSum="5" 表示这个LinearLayout总共平分成5块大小区域;然后再LinearLayout里面的控件,使用android:layout_wetght=“1”,这表示它占用整个布局的1/5。

提一个问题供读者思考思考一下:如果我们需要在一个LinearLayout里面,使用一个Button,这个Button的宽度要占到整个布局的一半,在XML中,要如何实现呢?

在这里,如果使用想写死Button的宽度的话,是不现实的,因为当你的手机设备不同时,屏幕的分辨率就不同了,所以是没办法实现这个效果的!那么,此时,我们就可以使用LinearLayout中的android:weightSum和android:layout_wetght属性了。此时,我只需要在XML中,为LinearLayout设置属性android:weightSum="2",然后在Button中设置属性android:layout_wetght=“1”,此时,Button将会占到整个布局的1/2,也就是布局宽度的一半了。我们想要的效果就达到了,而且不会因为手机设备不同,而不能适配。

时间: 2024-07-29 23:29:24

在LinearLayout中,巧妙使用weightSum和layout_weight的相关文章

Android UI: LinearLayout中layout_weight 属性的使用规则

首先来查看android sdk文档,有这么一段话 LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space is should occupy on the sc

LinearLayout中的layout_weight属性详解

线性布局中的layout_weight顾名思义即为定义各个组件在整个界面中所占的权重.常用两种实现方法 1:最为简单的实现方法,当orientation为水平时垂直layout_height=''0dp",同理当orientation为垂直时,将layout_width设定为0dp,此时在一个直接父布局中layout_width中的数值越大这个组件占的界面就越大.示例代码如下: <LinearLayout xmlns:android="http://schemas.android

weightSum和layout_weight属性合用

讲解一:weightSum和layout_weight属性合用 android:weightSum属性:定义weight总和的最大值.如果为指定该值,所有子视图的layout_weight属性的累加值作为总和的最大值.例如,通过指定子视图的layout_weight属性为0.5,并设置LinearLayout的weight属性为1.0,实现子视图占据可用宽度的50%: layout_weight属性:子视图的控件比例.就像我们在盒子放置其他物体,盒子可用空间的比例就是weightSum,盒子每个

日积月累:weightSum和layout_weight属性合用

解说一:weightSum和layout_weight属性合用 android:weightSum属性:定义weight总和的最大值. 假设为指定该值,全部子视图的layout_weight属性的累加值作为总和的最大值.比如.通过指定子视图的layout_weight属性为0.5.并设置LinearLayout的weight属性为1.0,实现子视图占领可用宽度的50%: layout_weight属性:子视图的控件比例.就像我们在盒子放置其它物体,盒子可用空间的比例就是weightSum,盒子每

让水平LinearLayout中的两个子元素分别居左和居右

前情提要:在LinearLayout中有两个子元素,LinearLayout的orientation是horizontal.需要让第一个元素居左,第二个元素居右 1.LinearLayout中默认的gravity属性是居左,所以默认两个子元素都是居左显示 2.设置第一个元素的layout_weight属性android:layout_weight="1",第二个元素的该属性不需要设置 这样子第一个元素会自适应行宽,并且挤推第二个元素,效果类似于第二个元素match_parent的翻转填

Android见招拆招六:LinearLayout中Gravity、Layout_Gravity何时生效

对于LinearLayout何时生效的问题 对于 LinearLayout 当 android:orientation="vertical"  时, 只有水平方向的设置才起作用,垂直方向的设置不起作用.即:left,right,center_horizontal 是生效的. 当 android:orientation="horizontal" 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用.即:top,bottom,center_vertical 是生效的

android中设置组件在LinearLayout中居中

在LinearLayout中让子控件居中,有两种: 1.在LinearLayout加上 android:gravity="center" 2.在子控件中加上 android:layout_gravity="center"

我的Android进阶之旅------&gt;如何在多个LinearLayout中添加分隔线

如果要适合于所有的Android版本,可以在多个LinearLayout放置用于显示分隔线的View.例如,放一个ImageView组件,然后将其背景设为分隔线的颜色或图像,分隔线View的定义代码如下: <ImageView android:layout_width="fill_parent" android:layout_height="1dp" android:background="#ffffff" /> 效果如下: 在And

我的Android进阶之旅------&amp;gt;怎样在多个LinearLayout中加入分隔线

假设要适合于全部的Android版本号,能够在多个LinearLayout放置用于显示分隔线的View. 比如,放一个ImageView组件.然后将其背景设为分隔线的颜色或图像,分隔线View的定义代码例如以下: <ImageView android:layout_width="fill_parent" android:layout_height="1dp" android:background="#ffffff" /> 效果例如以下