国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

安卓-使用layout_weight屬性自定義標(biāo)題欄布局

2019-11-09 15:11:33
字體:
供稿:網(wǎng)友

今天使用android:layout_weight這個屬性來實(shí)現(xiàn)一個標(biāo)題欄的布局。線上效果圖:

布局文件如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:id="@+id/id_parent"              android:background="#3385ff"              android:orientation="horizontal"              android:layout_width="match_parent"              android:layout_height="44dp">    <Button        android:layout_margin="5dp"        android:background="#FF0000"        android:textAllCaps="false"        android:id="@+id/id_btn_back"        android:text="back"        android:textColor="#FFFFFF"        android:textSize="12sp"        android:layout_gravity="center_vertical"        android:gravity="center"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>    <TextView        android:textAllCaps="false"        android:id="@+id/id_tv_title"        android:text="title"        android:textSize="15sp"        android:textColor="#FFFFFF"        android:layout_gravity="center_vertical"        android:gravity="center"        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="wrap_content"/>    <Button        android:textSize="12sp"        android:layout_margin="5dp"        android:background="#00FF00"        android:textAllCaps="false"        android:id="@+id/id_btn_more"        android:text="more"        android:textColor="#FFFFFF"        android:layout_gravity="center_vertical"        android:gravity="center"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/></LinearLayout>主要說下layout_weight的用法:

android:layout_weight只能用在線性布局中android:layout_weight的真實(shí)含義是:一旦View設(shè)置了該屬性(假設(shè)有效的情況下),那么該 View的寬度等于原有寬度(android:layout_width)加上剩余空間的占比!設(shè)屏幕寬度為L,在兩個view的寬度都為match_parent的情況下,原有寬度為L,兩個的View的寬度都為L,那么剩余寬度為L-(L+L) = -L, 左邊的View占比三分之一,所以總寬度是L+(-L)*1/3 = (2/3)L.事實(shí)上默認(rèn)的View的weight這個值為0,一旦設(shè)置了這個值,那么所在view在繪制的時候執(zhí)行onMeasure兩次的原因就在這。Google官方推薦,當(dāng)使用weight屬性時,將width設(shè)為0dip即可,效果跟設(shè)成wrap_content是一樣的。這樣weight就可以理解為占比了!


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 霍林郭勒市| 平阳县| 三亚市| 龙口市| 东乡族自治县| 资源县| 肇源县| 广元市| 延寿县| 措勤县| 泗洪县| 凌海市| 扶余县| 滨州市| 宜川县| 忻州市| 屏山县| 两当县| 郴州市| 长宁区| 石门县| 泸定县| 白银市| 尉犁县| 滦南县| 奉化市| 原平市| 囊谦县| 六盘水市| 瑞金市| 凌源市| 杭锦旗| 唐海县| 施甸县| 甘南县| 边坝县| 新沂市| 隆安县| 曲松县| 乐业县| 三亚市|