亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

日積月累:LinearLayout的andrid:layout_weight

系統(tǒng) 1829 0

在開發(fā)的過程中,為了布局更好的適配各種各樣的屏幕,會(huì)經(jīng)常使用android:layout_weight屬性,按比例分配屏幕的空間。在很多資料和書籍中解釋說,系統(tǒng)根據(jù)layout_weight比例分配占據(jù)空間的大小。但是這個(gè)解釋在實(shí)際開發(fā)過程中,往往給我們帶來許多困惑。

現(xiàn)在我們來看看具體場景如下:我們需要將三個(gè)TextView按照1:2:3的橫向的比例顯示。于是就有了如下代碼:

        <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" > 
 
    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:background="#FF4500" 
        android:gravity="center" 
        android:text="第一個(gè)" /> 
 
    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="2" 
        android:background="#D15FEE" 
        android:gravity="center" 
        android:text="第二個(gè)" /> 
 
    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="3" 
        android:background="#CAFF70" 
        android:gravity="center" 
        android:text="第三個(gè)" /> 
</LinearLayout> 
      
觀察結(jié)果顯示如下(我們分別修改三個(gè)TextView的laytou_width屬性 ):

1.layout_width: wrap_content

2.layout_width: fill_parent

3.layout_width:"0dp"

觀察可知,在三種情況下,同樣的layout_weight比例1:2:3,0dp的比例正確外,其它兩種產(chǎn)生了錯(cuò)誤的不同結(jié)果,這就讓我們非常疑惑。


在查閱官方文檔: Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight。意思是,子視圖可以指定一個(gè)權(quán)重值,然后在這個(gè)視圖組中的所有剩余空間根據(jù)它們聲明的權(quán)重比例分配

查閱相關(guān)的各方面資料,綜合總結(jié),layout_weight比例分配的原則如下

第一步,根據(jù)layout_width / layout_height ( 根據(jù)方向而定 ) 的屬性分配視圖的空間

第二步,計(jì)算視圖組剩余的空間大小,并按照layout_weight比例分配給子視圖

第三步,子視圖實(shí)際空間 = layout_width / layout_height分配空間+layout_weight比例分配空間

根據(jù)如上步驟我們分別解釋上面三種情況結(jié)果的原因

為了更好的解釋,這里我們聲明兩個(gè)常量content_width(內(nèi)容所需占據(jù)的空間大小)和screen_width(屏幕的寬度空間大小)。

1.layout_width: wrap_content

第一步,根據(jù)layot_width:wrap_content分配三個(gè)TextView內(nèi)容填充的空間,三個(gè)TextView的layout_width空間分別為:

layout_width1= content_width

layout_width2= content_width

layout_width3= content_width

第二步,視圖組線性布局剩余的空間大小 = screen_width - 3 * content_width;那么三個(gè)TextView 的layout_weight比例分配空間分別為

layout_weight1=(screen_width-3* content_width )/6

layout_weight2= ( screen_width-3* content_width )/3

layout_weight3= ( screen_width-3* content_width )/2

第三步,根據(jù)計(jì)算公式, 子視圖實(shí)際空間 = layout_width / layout_height分配空間+layout_weight比例分配空間 三個(gè)TextView的空間分別為

textview1= content_width + (screen_width-3* content_width )/6

textview2= content_width + (screen_width-3* content_width )/3

textview3= content_width + (screen_width-3* content_width )/2

那么textview1:textview2:textview3=(screen_width-2* content_width ):2*( screen_width-2* content_width ):3* screen_width。顯然,該比率是隨著content_width和screen_width而變化的,不能滿足我們的需求

2.layout_width: fill_parent

第一步,三個(gè)TextView的layout_width空間分別為

layout_width1= screen_width

layout_width2= screen_width

layout_width3= screen_width

第二步,三個(gè)TextView 的layout_weight比例分配空間分別為

layout_weight1=(screen_width-3* screen_width )/6=- screen_width /3

layout_weight2= ( screen_width-3* screen_width )/3=-2* screen_width /3

layout_weight3= ( screen_width-3* screen_width )/2=- screen_width

第三步,根據(jù)計(jì)算公式, 子視圖實(shí)際空間 = layout_width / layout_height分配空間+layout_weight比例分配空間 三個(gè)TextView的空間分別為

textview1= screen_width - screen_width /3 = 2 * screen_width /3

textview2= screen_width - 2 * screen_width /3 = screen _width/3

textview3= screen_width - screen_width = 0

那么textview1:textview2:textview3=2:1:0,符合我們觀察到的現(xiàn)象!但不滿足需求。

3.layout_width:"0dp"

第一步,三個(gè)TextView的layout_width空間分別為

layout_width1=0dp

layout_width2=0dp

layout_width3=0dp

第二步,三個(gè)TextView 的layout_weight比例分配空間分別為

layout_weight1=(screen_width-0dp)/6= screen_width /6

layout_weight2=( screen_width-0dp)/3= screen_width /3

layout_weight3=(screen_width-0dp)/2= screen_width /2

第三步,根據(jù)計(jì)算公式, 子視圖實(shí)際空間 = layout_width / layout_height分配空間+layout_weight比例分配空間 三個(gè)TextView的空間分別為

textview1= 0dp+screen_width/6= screen_width /6

textview2= 0dp+screen_width/3= screen_width /3

textview3= 0dp+screen_width/2= screen_width /2

那么textview1:textview2:textview3=1:2:3,符合我們觀察到的結(jié)果,完全滿足我們的需求!!!


綜上的分析和總結(jié),建議在開發(fā)的過程當(dāng)中,如果想根據(jù)自己的意愿,按照比例分配子視圖占據(jù)的空間,將你分配的方向的layout_width或layout_height屬性設(shè)置為”0dp"。

日積月累:LinearLayout的andrid:layout_weight屬性的使用詳解


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 大尺度视频网站久久久久久久久 | 中文字幕一区二区三区有限公司 | 国产精品网站 夜色 | 最近中文日本字幕免费完整 | 国产成人刺激视频在线观看 | 亚洲系列第一页 | 美女视频黄视大全视频免费网址 | 国产精品揄拍一区二区久久 | 久久国产精品国产自线拍免费 | 99热热久久这里只有精品166 | 亚洲三级欧美 | 一级肉体毛片视频免费看看 | 天天综合天天添夜夜添狠狠添 | 国产成人高清亚洲一区91 | 欧美毛片一级的免费的 | 玖玖国产精品视频 | 91成年人 | 婷婷国产成人久久精品激情 | 日韩精品一 | 亚洲欧美另类在线 | 国产免费播放 | 中文字幕免费视频精品一 | 91亚洲国产成人久久精品网站 | 色播在线 | 亚洲性在线 | 成人在线日韩 | 欧美亚洲国产精品久久久 | 成人精品视频一区二区三区 | 亚洲 欧美 视频 | 日韩一级欧美一级毛片在线 | a毛片免费观看完整 | 精品理论片一区二区三区 | 欧美成人在线视频 | 久久精品国产亚洲aa | 在线播放成人毛片免费视 | 91亚洲国产成人久久精品网址 | 青青青国产成人久久111网站 | 国内自拍网红在线综合 | 欧美大成色www永久网站 | 久青草国产观看在线视频 | 亚洲综合99 |