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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#66ff66"android:layout_weight="1"android:text="面碼" /><TextViewandroid: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"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:background="#66ff66"android:layout_weight="1"android:text="面碼" /><TextViewandroid: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(权重)的具体分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android gravity和layo
- 下一篇: LinearLayout (线性布局)的