日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

android weight(权重)的具体分析

發布時間:2023/11/27 生活经验 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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(权重)的具体分析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。