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

首頁 > 學院 > 開發設計 > 正文

安卓-使用layout_weight屬性自定義標題欄布局

2019-11-09 14:11:48
字體:
來源:轉載
供稿:網友

今天使用android:layout_weight這個屬性來實現一個標題欄的布局。線上效果圖:

布局文件如下:

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南漳县| 东源县| 阳东县| 磐安县| 西青区| 曲沃县| 米脂县| 霍邱县| 呼图壁县| 乡城县| 嘉义县| 庆云县| 浪卡子县| 丰都县| 安仁县| 林州市| 宁波市| 晋城| 清涧县| 乌鲁木齐市| 松阳县| 达州市| 襄汾县| 宣化县| 建德市| 昌乐县| 西贡区| 静乐县| 兰西县| 石门县| 老河口市| 桑日县| 万山特区| 台前县| 札达县| 遂昌县| 石景山区| 阿拉善盟| 九寨沟县| 宽甸| 西平县|