zoukankan      html  css  js  c++  java
  • 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 就是当前控件内容显示的位置。

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

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



    
    




  • 相关阅读:
    Jenkins的多个任务并串联参数传递
    jenkins任务构建失败重试插件Naginator Plugin
    通过hive向写elasticsearch的写如数据
    Elasticsearch中的索引管理和搜索常用命令总结
    Elasticsearch安装
    运行 Spark on YARN
    Jenkins console输出乱码???
    spark的standlone模式安装和application 提交
    多种排序算法整理
    结构型模式(二)
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/7101466.html
Copyright © 2011-2022 走看看