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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

从零开始学android开发-布局中 layout_gravity、gravity、orientation、layout_weight

發(fā)布時間:2023/12/18 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 从零开始学android开发-布局中 layout_gravity、gravity、orientation、layout_weight 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

線性布局中,有?4?個及其重要的參數(shù),直接決定元素的布局和位置,這四個參數(shù)是

android:layout_gravity (?是本元素相對于父元素的重力方向?)

android:gravity?(是本元素所有子元素的重力方向)

android:orientation?(線性布局以列或行來顯示內(nèi)部子元素)

android:layout_weight?(線性布局內(nèi)子元素對未占用空間【水平或垂直】分配權(quán)重值,其值越小,權(quán)重越大。

??????????????????????????? 前提是子元素?設(shè)置了?android:layout_width?=?"fill_parent"?屬性(水平方向)

????????????????????????? ? ???或?android:layout_height?=?"fill_parent"?屬性(垂直方向)

?

如果某個子元素的?android:layout_width?=?"wrap_content"

???????????或?android:layout_height ="?wrap_content”?

則?android:layout_weight?的設(shè)置值?對該方向上空間的分配剛好相反。

?

下面以一個簡單例子來說明這?4個參數(shù)

<??xml?version?=?"1.0"?encoding?=?"utf-8"??>

<?LinearLayout?xmlns:android?=?"http://schemas.android.com/apk/res/android"

???????????????????????????android:layout_height?=?"200dp"

???????????????????????????android:layout_width?=?"200dp"

???????????????????????????android:background?=?"#AABBCC"

???????????????????????????android:orientation=?"horizontal"

???????????????????????????android:layout_gravity=?"center"?>

???????????????????????????<?TextView?android:text?=?"ONE"

???????????????????????????????????????????????android:background?=?"#aa0000"

???????????????????????????????????????????????android:layout_height?=?"wrap_content"

???????????????????????????????????????????????android:layout_width?=?"wrap_content"

???????????????????????????????????????????????android:layout_margin?=?"1dp"?/>

????????????????????????????<?TextView?android:text?=?"TWO"

???????????????????????????????????????????????android:background?=?"#aa0000"

???????????????????????????????????????????????android:layout_height?=?"wrap_content"

???????????????????????????????????????????????android:layout_width?=?"wrap_content"

???????????????????????????????????????????????android:layout_margin?=?"1dp"?/>

</?LinearLayout?>

?

說明:在上面的例子中,根布局是LinearLayout,?其包含有2?個TextView?視圖,為了對參數(shù)?android:layout_gravity有直觀的了解,對根布局?LinearLayout?特意加了?3?個參數(shù)

android:layout_height?=?"200dp"

android:layout_width???=?"200dp"

android:background?????=?"#AABBCC"

為布局指定了固定的寬度和高度,以及背景顏色,上面的例子運行后效果如下圖:

?

?

說明:對LinearLayout?中的參數(shù)android:layout_gravity?來說,其意義是指定本布局相對于父布局的重力方向,由于該布局的已經(jīng)是根布局,其父布局是整個屏幕,那么該參數(shù)設(shè)置的是相對于屏幕的位置,可以換不同的參數(shù)top|bottom|left|right?等等參數(shù)來試驗。

現(xiàn)在增加參數(shù)?android:gravity?=?"bottom|right"?完整?XML?如下,看看效果

?

<??xml?version?=?"1.0"?encoding?=?"utf-8"??>

<?LinearLayout?xmlns:android?=?"http://schemas.android.com/apk/res/android"

??????????????????????android:layout_height?=?"200dp"

??????????????????????android:layout_width?=?"200dp"

??????????????????????android:background?=?"#AABBCC"

??????????????????????android:orientation="horizontal"

??????????????????????android:layout_gravity=?"center"

??????????????????????android:gravity?=?"bottom|right?"?>

??????????????????????<?TextView?android:text?=?"ONE"

? ???????????????????????????????????android:background?=?"#aa0000"

? ???????????????????????????????????android:layout_height?=?"wrap_content"

? ???????????????????????????????????android:layout_width?=?"wrap_content"

? ???????????????????????????????????android:layout_margin?=?"1dp"?/>

? ????????????????????<?TextView?android:text?=?"TWO"

? ???????????????????????????????????android:background?=?"#aa0000"

? ???????????????????????????????????android:layout_height?=?"wrap_content"

? ???????????????????????????????????android:layout_width?=?"wrap_content"

? ???????????????????????????????????android:layout_margin?=?"1dp"?/>

</?LinearLayout?>

?

通過改變android:gravity?參數(shù)的值可以看到實際效果。

?

參數(shù)?android:orientation=?"?horizontal?"?決定了每個子元素各占一列,如果

參數(shù)?android:orientation=?"?vertical?"?,?則每個子元素各占一行,也就是從上到下排列了。

?

對于?LinearLayout?布局的子元素,給每個子元素加上參數(shù)?android:layout_weight

看看效果

<??xml?version?=?"1.0"?encoding?=?"utf-8"??>

<?LinearLayout?xmlns:android?=?"http://schemas.android.com/apk/res/android"

? ????????????????????android:layout_height?=?"200dp"

? ????????????????????android:layout_width?=?"200dp"

? ????????????????????android:background?=?"#AABBCC"

? ????????????????????android:layout_gravity?=?"center"

? ????????????????????android:gravity?=?"bottom|right"

? ????????????????????android:orientation?=?"horizontal"?>

? ????????????????????<?TextView?android:text?=?"ONE"

? ???????????????????????????????????android:background?=?"#aa0000"

? ???????????????????????????????????android:layout_height?=?"wrap_content"

? ???????????????????????????????????android:layout_width?=?"wrap_content"

? ???????????????????????????????????android:layout_margin?=?"1dp"

? ???????????????????????????????????android:layout_weight?=?"1"?/>

? ????????????????????<?TextView?android:text?=?"TWO"

? ???????????????????????????????????android:background?=?"#aa0000"

? ???????????????????????????????????android:layout_height?=?"wrap_content"

? ???????????????????????????????????android:layout_width?=?"wrap_content"

? ???????????????????????????????????android:layout_margin?=?"1dp"

? ???????????????????????????????????android:layout_weight?=?"2"?/>

</?LinearLayout?>

?

?

?

Text?為ONE?的權(quán)重為1?,但明顯占的寬度比TWO?的小,百思不得其解,后來得知,如果把TextView?的參數(shù)android:layout_width?=?"wrap_content"?全部修改為?android:layout_width?=?"fill_parent",?則?ok?,代碼如下

?

<??xml?version?=?"1.0"?encoding?=?"utf-8"??>

<?LinearLayout?xmlns:android?=?"http://schemas.android.com/apk/res/android"

??????????????????????android:layout_height?=?"200dp"

??????????????????????android:layout_width?=?"200dp"

??????????????????????android:background?=?"#AABBCC"

??????????????????????android:layout_gravity?=?"center"

??????????????????????android:gravity?=?"bottom|right"

??????????????????????android:orientation?=?"horizontal"?>

??????????????????????<?TextView?android:text?=?"ONE"

?????????????????????????????????????android:background?=?"#aa0000"

?????????????????????????????????????android:layout_height?=?"wrap_content"

?????????????????????????????????????android:layout_width?=?"?fill_parent?"

?????????????????????????????????????android:layout_margin?=?"1dp"

?????????????????????????????????????android:layout_weight?=?"1"?/>

??????????????????????<?TextView?android:text?=?"TWO"

?????????????????????????????????????android:background?=?"#aa0000"

?????????????????????????????????????android:layout_height?=?"wrap_content"

?????????????????????????????????????android:layout_width?=?"?fill_parent?"

?????????????????????????????????????android:layout_margin?=?"1dp"

?????????????????????????????????????android:layout_weight?=?"2"?/>

</?LinearLayout?>

?

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

總結(jié)

以上是生活随笔為你收集整理的从零开始学android开发-布局中 layout_gravity、gravity、orientation、layout_weight的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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