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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

Android(java)学习笔记96:layout_weight使用注意事项

發(fā)布時(shí)間:2023/11/27 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android(java)学习笔记96:layout_weight使用注意事项 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.?android:layout_weight使用說明:

layout_weight是權(quán)重的意思,也就是各個(gè)控件所占的比重,用在LinearLayout布局中。當(dāng)我們使用layout_weight的時(shí)候,layout_width和layout_height有三種表示方法

?

2.?android:layout_weight使用之?layout_width為0dp

此時(shí),layout_weight使用的時(shí)候要求:layout_height = "0" ?或者 ?layout_width = "0"

比如:只有如下這樣layout_weight屬性才會(huì)生效

(1)android:layout_width = "0dip"

? ? ? ? ?android:layout_weight = "1"

(2)android:layout_height = "0dip"

? ? ? ? ?android:layout_weight = "1"

下面是一個(gè)案例:

首先看看我們的布局需求,如下:

如下布局中上下各占1/2,然后這兩個(gè)一半,分別如下方式分割為1/3

布局代碼如下

 1 <?xml version="1.0"  encoding="utf-8"?>
 2 <LinearLayout xmln:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:oritentation="vertical">
 6     
 7     <LinearLayout
 8          android:layout_width="match_parent"
 9      android:layout_height="0dip"
10      android:layout_weight="1"
11          android:oritentation="horizontal">
12          
13         <View
14             android:layout_width="0dip"
15             android:layout_height="match_parent"
16             android:background="#ff0000"
17        android:layout_weight="1"
18            />
19         <View
20             android:layout_width="0dip"
21             android:layout_height="match_parent"
22             android:background="#0000ff"
23        android:layout_weight="1"
24            />
25         <View
26             android:layout_width="0dip"
27             android:layout_height="match_parent"
28             android:background="#00ff00"
29        android:layout_weight="1"
30            />
31     </LinearLayout>     
32     <LinearLayout
33          android:layout_width="match_parent"
34       android:layout_height="0dip"
35       android:layout_weight="1"
36          android:oritentation="vertical">
37          
38          <View
39             android:layout_width="match_parent"
40        android:layout_height="0dip"
41        android:layout_weight="1"
42             android:background="#ff00ff"
43            />
44         <View
45             android:layout_width="match_parent"
46        android:layout_height="0dip"
47        android:layout_weight="1"
48             android:background="#00ffff"
49            />
50         <View
51             android:layout_width="match_parent"
52        android:layout_height="0dip"
53        android:layout_weight="1"
54             android:background="#ffff00"
55            />
56     </LinearLayout>    
57     
58 </LinearLayout>    

?

3.?android:layout_weight使用之 layout_width為wrap_content


我們可以看出,首先他們是先包含自己的內(nèi)容,然后在剩下的空間中按照weight來劃分,即剩下空間按照1:2來劃分。

?

4.?android:layout_weight使用之?layout_width為match_parent

?

我們可以看出,剛好和第一種layout_width為0dp的布局相反。

?

?

4. 以上三種情況到底是怎么回事呢?下面來看一下layout_weight的具體計(jì)算方法:

layout_weight的意思:

如果一個(gè)控件申明了這個(gè)屬性,那么這個(gè)控件的寬度(高度)等于它原有的長(zhǎng)度(寬度或高度)加上剩余空間所占有的比重。

例如:一個(gè)假設(shè)屏幕的寬度為L(zhǎng).

(1)對(duì)于第一種情況:

原有長(zhǎng)度都等于0,所以它們的長(zhǎng)度就是剩余空間所占有的比重。

第一個(gè)button所占有的比重為L(zhǎng)*1/(1+2)=1/3L

第二個(gè)button所占有的比重為L(zhǎng)*2/(1+2)=2/3L

?

(2)對(duì)于第三種情況:

原有長(zhǎng)度都等于match_parent即等于L,那么剩余空間的長(zhǎng)度等于L-(L+L)=-L;

對(duì)于第一個(gè)button所占有的比重為:-L*1/(1+2)=-1/3L

對(duì)于第二個(gè)button所占有的比重為:-L*2/(1+2)=-2/3L,所以它們的總長(zhǎng)度等于原有的長(zhǎng)度加上剩余空間所占有的比重。

即L+(-1/3L)= 2/3L 和 L+(-2/3L)= 1/3L;

即反過來的2:1就是出現(xiàn)的第三種情況。

?

轉(zhuǎn)載于:https://www.cnblogs.com/hebao0514/p/4729591.html

總結(jié)

以上是生活随笔為你收集整理的Android(java)学习笔记96:layout_weight使用注意事项的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。