LinearLayout关于weight使用问题

今天在解决安卓应用兼容性问题,问题是由于使用LinearLayout的weight引起的,下面我使用一个Demo简述一下问题以及解决方法。

样例1:使用weight的是布局文件,包含TextView

<LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/spinner_bg"
        android:gravity="center" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="测试1" />
    </LinearLayout>

测试效果:小米A2以及红米Note HD两个手机的样式都一样,如下

样例2:使用weight的是布局文件,包含CheckBox,第一个替换

 <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/spinner_bg"
        android:gravity="center" >

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:gravity="center"
            android:text="测试1" />
    </LinearLayout>

测试效果:

小米A2测试效果: 红米Note测试效果:

从上面的测试结果来看小米A2的替换为CheckBox的第一个内容向右偏移了,红米Note正常。如果我们只针对布局参数来调整的话,调试不出来居中效果,但是如果你设置了CheckBox的背景就可以了,这个可以使 android:background="@android:color/transparent",设置之后就会居中对齐,不仅CheckBox有这个问题,Radiobutton也有这个问题,因为在项目里面正好都用到,而且出现 了相同的问题,不居中。整个代码如下:

<LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/spinner_bg" >

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:background="@android:color/transparent"
            android:button="@null"
            android:text="测试1" />
    </LinearLayout>

可能有人会说你直接用CheckBox就可以了,为什么还要加LinearLayout包一层,这个是由于项目效果问题,下面我截图演示一下,对于我的项目加和不加一层布局的区别,下面是项目用到的两个截图,这里我介绍一下左边标题是“精选推荐”的。


"精选推荐"是使用CheckBox+drawableRight来实现文字加下三角,看到UI,很多人会想到TextView+drawableRight,我这个项目的功能问题,我选择的是CheckBox+drawableRight来实现,如果看CheckBox源码的话,最后CheckBox也是TextView一个子类,接下来只使用CheckBox+drawableRight来作为item,

<CheckBox
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/spinner_bg"
        android:button="@null"
        android:drawableRight="@drawable/tri_normal"
        android:gravity="center"
        android:text="测试1" />

显示效果如下:

,结果右边的小三角跑到最右边了,用户体验性不太好,也与项目UI不符合,所以加一个父布局,修改代码如下:

,这样的话就跟Spinner一样了,由于到兼容性以及项目用到的下拉有二级菜单,所以就没有使用Spinner来开发。

对于CheckBox以及RadioButton这一类的组件,如果父布局使用了weight后,需要设置其背景当然也可以设置透明,这样位置属性才能用。

时间: 2024-08-28 17:42:25

LinearLayout关于weight使用问题的相关文章

LinearLayout设置 weight 无法绘制的问题

项目地址:ChildLayout 一. 问题起因: 新项目中首页有这么一个需求:看布局: 主要就是"大牌专场"这个栏目的布局问题,呈现一个,左--右上--右下 的形式,因为不能像 iOS 那样直接根据 UED 给的标注来写死布局尺寸,Android 混乱的分辨率决定了这个布局需要采用 android:layout_height="wrap_content" 的形式来决定它的高. 二. 解决方法1:使用 LinearLayout--失败 要画这个布局很简单的,比例都是

LinearLayout的weight属性的解释

刚刚在stackoverflow上看到一个关于android LinearLayout的weight属性的解释,觉得解释很透彻,帖过来和大家分享一下. In a nutshell, layout_weight specifies how much of the extra space in the layout to be allocated to the View. LinearLayout supports assigning a weight to individual children.

寒假学干货之------LinearLayout.layout.weight

所有原始代码由这个大神写的--http://www.cnblogs.com/zhangs1986/archive/2013/01/17/2864237.html layout/activity_main下 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:i

android之线性布局LinearLayout以及weight权重使用

LinearLayout(线性布局): :layout/activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:lay

Android LinearLayout中weight属性的意义与使用方式

layout_weight 分割父级容器的比例 效果 调整一下: 不设置则不参与父级容器的分割,设置了以后则参与

Android Hack1 使用weight属性实现视图的居中显示

本文地址:http://www.cnblogs.com/wuyudong/p/5898403.html,转载请注明源地址. 如果要实现如下图所示的将按钮居中显示,并且占据父视图的一半,无论屏幕是否旋转 合用weightSum属性和layout_weight属性 不同Android设备的尺寸往往是不同的.作为开发者,我们需要创建适用于不同尺寸屏幕的XML文件.硬编码是不可取的,因此需要其他方法来组织视图. 本文分析如何合用layout_weight和weightSum这两个属性来填充布局内部的任意

2.2.1 LinearLayout(线性布局)

本节引言 本节开始讲Android中的布局,Android中有六大布局,分别是: LinearLayout(线性布局),RelativeLayout(相对布局),TableLayout(表格布局) FrameLayout(帧布局),AbsoluteLayout(绝对布局),GridLayout(网格布局) 而今天我们要讲解的就是第一个布局,LinearLayout(线性布局),我们屏幕适配的使用 用的比较多的就是LinearLayout的weight(权重属性),在这一节里,我们会详细地解析 L

【Android 开发笔记】weight属性

混合使用weightSum和layout_weight 先看效果,button占据屏幕宽度的一半. 再看开发文档中的描述. "定义weight总和的最大值.如果未指定该值,以所有子视图的layout_weight属性的累加值作为总和的最大值.一个典型的案例是:通过指定子视图的layout_weight属性为0.5,并设置LinearLayout的weightSum属性为1.0,实现子视图占据可用宽度的50." XML文件的源码. <LinearLayout xmlns:andro

如何优化你的布局层级结构之RelativeLayout和LinearLayout及FrameLayout性能分析

转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/51159419 如何优化你的布局层级结构之RelativeLayout和LinearLayout及FrameLayout性能分析 工作一段时间后,经常会被领导说,你这个进入速度太慢了,竞品的进入速度很快,你搞下优化吧?每当这时,你会怎么办?功能实现都有啊,进入时要加载那么多view,这也没办法啊,等等. 先看一些现象吧:用Android studio,新建一个Activity自动生成的