android weight(权重)的详细分析

首先要明确权重分配的是那些空间?

权重是依照比例分配屏幕的剩余空间

对这句话不理解的能够看下图

假如我们希望剩余的空间平分给空间1 和空间2 ,

我们分别在2个控件的设置android:layout_weight="1"

上面算是对权重的分析,详细使用方法例如以下

先看一段代码吧

<span style="font-size:32px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#66ff66"
        android:layout_weight="1"
        android:text="面码" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        android:layout_weight="1"
        android:text="小木" />

</LinearLayout></span>

这段代码非常简答。这里是水平方向为样例的。

我就说下android:weightSum="2"

这个是权重分的总个数。这里我分为2分,

这个能够要能够不要。当你对权重不是非常理解的的话建议要

上面代码的效果图

我把背景颜色设置不同,方便大家看呢,这时候两者是平分的,

原因是控件的初始长度一样,都是wrap_content,为了便于区分

权重分配的是剩余的空间。把初始长度设置为不一样,看以下代码

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

android:weightSum="2">

<TextView

android:layout_width="120dp"

android:layout_height="wrap_content"

android:background="#66ff66"

android:layout_weight="1"

android:text="面码" />

<TextView

android:layout_width="30dp"

android:layout_height="wrap_content"

android:background="#ff0000"

android:layout_weight="1"

android:text="小木" />

</LinearLayout>

效果图

非常明显不一样了,原因也就是两者控件初始化长度

不一样,把剩余的空间平分给他们之后他们的

长度于是会不一样的

以上就是整个项目布局完之后我对权重的理解,

对了提一下,项目中我一般设置 android:layout_width="0dp"

代码还用刚才的吧

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="2">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#66ff66"

        android:layout_weight="1"
        android:text="面码" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#ff0000"

        android:layout_weight="1"
        android:text="小木" />

</LinearLayout>

为啥要把长度设置为0呢?这就要从我写的第一句话看是想了

那就为了为了能更好的分配剩余的空间。忽略掉初始的长度。

以上就是我的理解,

补充下http://blog.csdn.net/qq_33210042/article/details/50902052

那在上张图片

开发中会常常遇到把字放到控件的中间在用viewgroup滑动同一时候

能改变字体的颜色。详细的实现就不说了,这里说下布局

我们要的就是这种把。左右滑动点击同一时候也能切换,

当然有时候不止2个那就把权重多分几份。

看下布局的代码

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

android:weightSum="2">

<TextView

android:layout_width="0dp"

android:layout_height="wrap_content"

android:background="#66ff66"

android:gravity="center"

android:layout_weight="1"

android:text="面码" />

<TextView

android:layout_width="0dp"

android:layout_height="wrap_content"

android:background="#ff0000"

android:gravity="center"

android:layout_weight="1"

android:text="小木" />

</LinearLayout></span>

用到了

android:gravity 就是当前控件内容显示的位置。

这里我是顺便提下,假设换有对不布局不理的童鞋

关注我的博客。我会个大家一同进步的。

				
时间: 2024-08-05 21:58:52

android weight(权重)的详细分析的相关文章

android关于AndroidManifest.xml详细分析

转:http://www.cnblogs.com/zady/archive/2013/10/14/3368385.html 一.关于AndroidManifest.xmlAndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activities, services, 等等),他们各自的实现类,各种能被处理的数据和启动位置. 除了能声明程序中的Activities, ContentProviders, Service

Android中资源管理机制详细分析

尊重原创:http://blog.csdn.net/yuanzeyao/article/details/42386549 在Android中,所有的资源都在res目录下存放,包括drawable,layout,strings,anim等等,当我们向工程中加入任何一个资源时,会在R类中相应会为该 资源分配一个id,我们在应用中就是通过这个id来访问资源的,相信做过Andorid开发的朋友对于这些肯定不会陌生,所以这个也不是我今天想要说的,我今天想和大家一起学习的是Android是如何管理资源的,在

Android的断点下载详细分析二

由于一篇blog写不完,这里是接着上一篇blog的. 写完了MVC中的View,写着我们需要考虑Control层了,他的任务是在后台利用多线程实现断点下载. 先看源码: public class FileDownloader { /* TAG,便于调试 */ private static final String TAG = "FileDownloader"; /* 上下文 */ private Context context; /* 用于对数据库的操作 */ private File

Android 布局之LinearLayout 子控件weight权重的作用详析(转)

关于Android开发中的LinearLayout子控件权重android:layout_weigh参数的作用,网上关于其用法有两种截然相反说法: 说法一:值越大,重要性越高,所占用的空间越大: 说法二:值越大,重要性越低,所占用的空间越小. 到底哪个正确?哪个错误?抑或还有其他解释?请点击查看关于weight 权重参数作用的详分析: 其实这两种情况都不太准确: 准确的解释是,weight 权限 是用于分配父控件某一方向上尺寸-所有子控件在该方向上设定尺寸和 所得值的一个参数,把这个相减得到的结

Android 布局之LinearLayout 子控件weight权重的作用详析

关于Android开发中的LinearLayout子控件权重android:layout_weigh参数的作用,网上关于其用法有两种截然相反说法: 说法一:值越大,重要性越高,所占用的空间越大: 说法二:值越大,重要性越低,所占用的空间越小. 到底哪个正确?哪个错误?抑或还有其他解释?请点击查看关于weight 权重参数作用的详分析: 其实这两种情况都不太准确: 准确的解释是,weight 权限 是用于分配父控件某一方向上尺寸-所有子控件在该方向上设定尺寸和 所得值的一个参数,把这个相减得到的结

Android版xx助手之天天酷跑外挂详细分析

Android版xx助手之天天酷跑外挂详细分析 图/文      莫灰灰 背景 近些年来,移动互联网的大肆崛起,潜移默化中影响着人们的生活和工作习惯.当腾讯的微信平台接入手机游戏之后,移动端的游戏也开始火了起来,这更是改变了人们长久以来的游戏娱乐习惯.茶余饭后,小伙伴们掏出"家伙"打个飞机已是习以为常的事情了.加之移动客户端游戏开发周期短,投入少等特点,很多初创公司也纷纷投入到这个领域中来,并且很多游戏都取得了不错的成绩.就在前不久,全球游戏巨头暴雪的新游戏<炉石传说>也推

Android BroadcastAnyWhere(Google Bug 17356824)漏洞详细分析

h1, h2, h3, h4, h5, h6, p, blockquote { margin: 0; padding: 0; } body { font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", Arial, sans-serif; font-size: 13px; line-height: 18px; color: #737373; background-color: white; margi

Android PackageManagerService详细分析

本篇主要分析了系统启动阶段包管理服务的启动流程,其中的几个接口在 apk 安装时也会被调用.包管理服务启动时主要做的工作大致有如下几方面: 1. 建立 java 层的 installer 与 c 层的 installd 的 socket 联接,使得在上层的 install,remove,dexopt等功能最终由 installd 在底层实现 2. 建 立 PackageHandler 消 息 循 环 , 用 于 处 理 外 部 的 apk 安 装 请 求 消 息 , 如 adbinstall,p

OpenStack_Swift源码分析——创建Ring及添加设备源码详细分析

1 创建Ring 代码详细分析 在OpenStack_Swift--Ring组织架构中我们详细分析了Ring的具体工作过程,下面就Ring中增加设备,删除设备,已经重新平衡的实现过程作详细的介绍. 首先看RingBuilder类 def __init__(self, part_power, replicas, min_part_hours): #why 最大 2**32 if part_power > 32: raise ValueError("part_power must be at

Cocos2d-x3.3RC0的Android编译Activity启动流程分析

本文将从引擎源代码Jni分析Cocos2d-x3.3RC0的Android Activity的启动流程,以下是具体分析. 1.引擎源代码Jni.部分Java层和C++层代码分析 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXV4aWt1b18x/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" > watermark/2/text/aHR0cDov