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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

浅谈Android布局

發(fā)布時間:2025/4/5 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 浅谈Android布局 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

? ? ? ? 在前面的博客中,小編介紹了Android的極光推送以及如何實(shí)現(xiàn)登錄的一個小demo,對于xml布局頁面,擺控件這塊的內(nèi)容,小編還不是很熟練,今天小編主要簡單總結(jié)一下在Android中的布局,學(xué)習(xí)過Android的小伙伴都知道,在安卓中有五大常用的布局,如下圖所示:

? ? ? ??

? ? ? ? 接著,小編就來詳細(xì)介紹這幾種布局,小編是初學(xué)者,還請各位小伙伴多多指教哦。首先,我們來看:

? ? ? ? 第一個LinearLayout---線性布局,線性布局是我們在開發(fā)Android項(xiàng)目中最常用的的一種布局方式,線性布局的方向有兩種,分別是垂直布局和水平布局,當(dāng)垂直布局時,每一行就只有一個元素,多個元素依次垂直往下;水平布局時,只有一行,每一個元素依次向右排列。我們來看一個具體的例子,代碼如下所示:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><LinearLayout android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:orientation="horizontal"><TextView android:text="blue"android:gravity="center_horizontal"android:background="#52C8FA"android:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_weight="1"/><TextViewandroid:text="yellow"android:gravity="center_horizontal"android:background="#FFFF00"android:layout_width="wrap_content"android:layout_height="fill_parent" android:layout_weight="1"/><TextViewandroid:text="pink"android:gravity="center_horizontal"android:background="#F60C88"android:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_weight="1"/><TextView android:text="purple"android:gravity="center_horizontal"android:background="#722694"android:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_weight="1"/> </LinearLayout><LinearLayoutandroid:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"><TextViewandroid:text="green"android:textSize="15pt"android:background="#39E18A"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"/><TextView android:text="pink" android:textSize="15pt" android:background="#F60C88" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:text="yellow" android:textSize="15pt" android:background="#FFFF00" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:text="blue" android:textSize="15pt" android:background="#52C8FA" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout></LinearLayout> ? ? ? ?效果如下圖所示:

? ? ? ?

? ? ? ?第二個FrameLayout---幀布局,幀布局是從屏幕的左上角(0,0)坐標(biāo)開始布局,多個組件層疊排列,第一個添加的組件放到最底層,最后添加到框架中的視圖顯示在最上面。上一層的會覆蓋下一層的控件,代碼如下所示:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><TextViewandroid:layout_width="300dp" android:layout_height="300dp" android:background="#52C8FA"/><TextView android:layout_width="260dp" android:layout_height="260dp" android:background="#FFFF00"/> <TextView android:layout_width="220dp" android:layout_height="220dp" android:background="#F60C88"/> </FrameLayout> ? ? ? ? 運(yùn)行效果如下所示:

? ? ? ??

? ? ? ?第三個TableLayout---表格布局,表格布局是一個ViewGroup以表格顯示它的子視圖(view)元素,即行和列標(biāo)識一個視圖的位置,每一個TableLayout里面有表格行TableRow,TableRow里面可以具體定義每一個元素。每一個布局都有自己適合的方式,這五個布局元素可以相互嵌套應(yīng)用,代碼如下所示:

?

<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><TableRow> <Button android:text="等" android:background="#52C8FA"/> <Button android:text="一" android:background="#FFFF00"/> <Button android:text="個" android:background="#F60C88"/> </TableRow> <TableRow> <Button android:text="故" android:background="#722694"/> <Button android:layout_span="2" android:text="事!" android:background="#39E18A"/> </TableRow> </TableLayout>

? ? ? ?運(yùn)行效果如下圖所示:

? ? ? ?

? ? ? ?第四個RelativeLayout---相對布局,相對布局是按照組件之間的相對位置來布局,比如在某個組件的左邊,右邊,上面和下面,相對布局可以理解為某一個元素為參照物,來定位的布局方式,具體代碼如下所示:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10px" > <TextView android:id="@+id/tev1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="30dp" android:text="請輸入口令,會有驚喜哦`(*∩_∩*)′:" /> <EditText android:id="@+id/tx1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/tev1" /> <Button android:id="@+id/btn1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="@id/tx1" android:layout_alignParentRight="true" android:text="確定" android:background="#FFFF00"/> <Button android:id="@+id/btn2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="@id/tx1" android:layout_toLeftOf="@id/btn1" android:layout_marginRight="30dp" android:text="取消" android:background="#FFFF00"/> </RelativeLayout> ? ? ? ?效果如下圖所示:

? ? ? ?
? ? ? ?第五個AbsoluteLayout---絕對布局,?絕對布局通過指定子組件的確切X,Y坐標(biāo)來確定組件的位置,在Android2.0 API文檔中標(biāo)明該類已經(jīng)過期,可以使用FrameLayout或者RelativeLayout來代替,小編就不在進(jìn)行相關(guān)介紹了,有需要的小伙伴可以動動自己可愛的小手,在網(wǎng)絡(luò)這個大世界里面尋找哦`(*∩_∩*)′!

? ? ? ?小編寄語:該博客,小編主要簡單的介紹了Android中常用的布局,看著一個一個代碼運(yùn)行成功,小編心里很是高興,為了慶祝一下,今天晚上不吃黃燜雞了,從開始封閉開發(fā)項(xiàng)目到現(xiàn)在,小編天天吃黃燜雞,這輩子都不想吃了,雖然這些對大牛們來說不值得一提,但正是由于了這一步又一步小小的進(jìn)步,小編才會成長的更加茁壯,更加美麗。

總結(jié)

以上是生活随笔為你收集整理的浅谈Android布局的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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