1. 程式人生 > >android weight(權重)的詳細分析

android weight(權重)的詳細分析

關註 一段 sum 比例 .net orien 2個 背景 wid

首先要明確權重分配的是那些空間?

權重是依照比例分配屏幕的剩余空間

對這句話不理解的能夠看下圖

技術分享

假如我們希望剩余的空間平分給空間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 就是當前控件內容顯示的位置。

這裏我是順便提下,假設換有對不布局不理的童鞋

關註我的博客。我會個大家一同進步的。







android weight(權重)的詳細分析