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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android layout_width 属性,android:layout_weight属性详解

發布時間:2025/3/11 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android layout_width 属性,android:layout_weight属性详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在android開發中LinearLayout很常用,LinearLayout的內控件的android:layout_weight在某些場景顯得非常重要,比如我們需要按比例顯示。android并沒用提供table這樣的控件,雖然有TableLayout,但是它并非是我們想象中的像html里面的table那么好用,我們常用ListView實現table的效果,但是列對齊確比較麻煩,現在用LinearLayout及屬性android:layout_weight能很好地解決。下面我們共同體驗下layout_weight這個屬性。一、LinearLayout內的控件的layout_width設置為"wrap_content",請看一下xml配置:

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1"

>

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="#aa0000"

android:gravity="center"

android:text="1"/>

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="2"

android:background="#00aa00"

android:gravity="center"

android:text="1"/>

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="3"

android:background="#0000aa"

android:gravity="center"

android:text="1"/>

** 效果如下:**

android:layout_weight屬性詳解

可以看到這三個TextView是按照1:2:3的比例進行顯示的,這樣看來似乎可以實現按照比例顯示了,但是有個問題,如果TextView內的文本長度一同那么較長文本的TextView會寬度會有所增加,見下面配置及效果:

配置:

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="#aa0000"

android:gravity="center"

android:text="1111111111111111111111111111111111111111111"/>

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="2"

android:background="#00aa00"

android:gravity="center"

android:text="2"/>

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="3"

android:background="#0000aa"

android:gravity="center"

android:text="3"/>

效果:

android:layout_weight屬性詳解

這樣看來我們所需要的按比例又無法實現了,經過滿天地google終于找到了解決方案,就是設置layout_width設置為"wrap_content"。配置及效果見下:

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

android:layout_width="0dp"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="#aa0000"

android:gravity="center"

android:text="1111111111111111111111111111111111111111111"/>

android:layout_width="0dp"

android:layout_height="fill_parent"

android:layout_weight="2"

android:background="#00aa00"

android:gravity="center"

android:text="2"/>

android:layout_width="0dp"

android:layout_height="fill_parent"

android:layout_weight="3"

android:background="#0000aa"

android:gravity="center"

android:text="3"/>

效果:

android:layout_weight屬性詳解

這樣終于達到我們的按比例顯示的效果了,感覺很是奇怪,android開發框架的大佬們有時候設計確實有點匪夷所思。

二、LinearLayout內的控件的layout_width設置為"fill_parent",請看一下xml配置:

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="#aa0000"

android:gravity="center"

android:text="1"/>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="2"

android:background="#00aa00"

android:gravity="center"

android:text="2"/>

效果如下:

android:layout_weight屬性詳解

奇怪吧,整個寬度平分3塊,第一個TextView占了兩塊,這樣看來weight值越小的優先級越大。只有兩個TextView似乎看出些道理,那么讓我們看看三個是什么效果:

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="#aa0000"

android:gravity="center"

android:text="1"/>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="2"

android:background="#00aa00"

android:gravity="center"

android:text="2"/>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="3"

android:background="#0000aa"

android:gravity="center"

android:text="3"/>

效果:

android:layout_weight屬性詳解

什么意思?第三個TextView丟掉了,很是奇怪,讓我們再試一個,把weight分別改為2,3,4的看看效果:

android:layout_weight屬性詳解

這個效果讓人很困惑,我一直想尋求出一個確切的比例公式,但是至今未找到。有哪位大神能搞定的話忘不吝賜教。

雖然這個android:layout_weight屬性很怪異,但幸運的是我們達到了目標:

按比例顯示LinearLayout內各個子控件,需設置android:layout_width="0dp",如果為豎直方向的設置android:layout_height="0dp"。在這種情況下某子個控件占用LinearLayout的比例為:本控件weight值 / LinearLayout內所有控件的weight值的和。

總結

以上是生活随笔為你收集整理的android layout_width 属性,android:layout_weight属性详解的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 亚洲精品人 | 18深夜在线观看免费视频 | 欧美一区二区三区啪啪 | 精品国产乱码久久久久久影片 | 双性人bbww欧美双性 | 在线观看亚洲一区二区 | 久久93 | 免费久久久久 | 桃色激情网 | 国产污污| 欧美人与性动交α欧美精品 | a级片免费看 | 丰腴饱满的极品熟妇 | 欧美一级乱黄 | 男人午夜剧场 | jizz高潮 | 亚洲av少妇一区二区在线观看 | 自拍偷拍第一页 | 久久亚洲影视 | 亚洲第一色网 | 国产av无码专区亚洲a∨毛片 | 国产又黄又爽又色 | 欧美色图久久 | 欧美日韩综合 | 天堂√ | 人人人超碰| 亚洲午夜伦理 | 日韩视频网站在线观看 | 91a视频| 免费在线观看黄色 | 日本bbwbbw| 亚洲自拍偷拍色图 | 日韩毛片网| 在线观看日韩欧美 | 四虎婷婷 | 免费看裸体视频网站 | 成人性生活毛片 | 中文字幕一区二区三区在线视频 | 91调教打屁股xxxx网站 | 奴性白洁会所调教 | 天天草夜夜草 | 男女拍拍拍 | 91精品国产99久久久久久红楼 | 少妇激情一区二区三区 | 少妇一级淫片 | www.色婷婷| 国产911视频 | 久久香蕉网 | 国产91亚洲| 亚洲综合成人av | 国产亚洲小视频 | 激情小说五月天 | 黄色永久免费网站 | 性感美女一区 | 日韩精品一区二区三区国语自制 | 亚洲精品91在线 | 国产精品91在线观看 | 国产熟女一区二区三区四区 | 欧美精品成人久久 | 日韩一区二区免费视频 | 少妇被黑人到高潮喷出白浆 | 国产亚洲欧美日韩高清 | 国产高清免费在线播放 | xxxxx黄色片 噜噜噜噜噜色 | 影音先锋亚洲精品 | 精品国产系列 | 中文字幕av一区二区三区 | 亚洲AV无码阿娇国产精品 | 亚洲图片视频小说 | 久久久久人妻一区精品色 | 午夜在线视频免费观看 | 天天曰夜夜曰 | 99爱这里只有精品 | 人人人人干 | 深爱五月网 | 91亚洲一线产区二线产区 | 美女午夜激情 | 亚洲精品少妇久久久久久 | 91精品免费视频 | 啪啪免费网 | 国产一区二区三区网站 | 女性向av免费网站 | 久久这里有精品视频 | 亚洲区小说区图片区qvod | 99久草| 夜夜免费视频 | 成人三级影院 | av在线第一页 | 四虎在线观看视频 | 网站在线播放 | 在线观看不卡一区 | 先锋影音资源av | 色姑娘综合 | 亚洲欧洲色 | 日本在线观看视频网站 | 精品久久久久一区 | 黄色激情四射 | 男操女视频在线观看 | 久久99一区|