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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【android-tips】android xml布局总结篇

發布時間:2025/4/5 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【android-tips】android xml布局总结篇 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

(轉載請注明出處:http://blog.csdn.net/buptgshengod

一.背景

? ? ? ? 可能很多人跟我一樣,做了好久的android程序,卻一直沒有認真地坐下來好好學習下xml文件的布局。其實有的時候我們用view繪制或是利用ADT的圖形界面功能就可以輕松搞定布局,但是最好還是靜下來學習下xml的布局文件具體寫法。這一節我們要繪制如下圖所示的界面。

二基礎知識

? ? ? ?首先我們要了解android到底有那些布局,和每個布局類型的區別。

?1.線性布局?LinearLayout

? ? ? ? ?線性布局分兩種。一種是水平布局,一種是垂直布局。下面我們根據上圖舉例子。 ? ? ? ? ?先把上圖的代碼貼出來吧! <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical"android:layout_width="fill_parent" android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"><TextView android:layout_height="wrap_content" android:text="@string/note_title"android:layout_width="wrap_content" android:padding="10dp"></TextView><LinearLayout android:layout_height="fill_parent"android:layout_width="fill_parent" android:layout_weight="1"><EditText android:id="@+id/EditText02" android:layout_width="fill_parent"android:layout_height="fill_parent" android:gravity="left"android:hint="@string/edithint"></EditText></LinearLayout><LinearLayout android:layout_height="fill_parent"android:layout_width="fill_parent" android:layout_weight="2"android:gravity="center"

android:orientation="horizontal"

><ImageButton android:id="@+id/ImageButton01"android:layout_width="72dp" android:layout_height="72dp"android:src="@drawable/sketchy_paper_003" android:layout_margin="3dp"></ImageButton><ImageButton android:id="@+id/ImageButton02"android:layout_width="72dp" android:layout_height="72dp"android:src="@drawable/sketchy_paper_004" android:layout_margin="3dp"></ImageButton><ImageButton android:id="@+id/ImageButton03"android:layout_width="72dp" android:layout_height="72dp"android:src="@drawable/sketchy_paper_007" android:layout_margin="3dp"></ImageButton><ImageButton android:id="@+id/ImageButton04"android:layout_width="72dp" android:layout_height="72dp"android:src="@drawable/sketchy_paper_011" android:layout_margin="3dp"></ImageButton></LinearLayout> </LinearLayout>
可以看到,上圖是由三部分組成。在大的LinearLayout從上而下垂直分布著三個內容:TextView,LinearLayout,LinearLayout。所以總體的LinearLayout是垂直布局android:orientation="vertical"
下面我們來看水平布局 其實就是上圖中的最下面那個LinearLayout。四個圖標平行排列。 android:orientation="horizontal"

2.相對布局 RelativeLayout

這個布局相對簡單一點。一般來講利用ADT自己拖放按鈕就可以。基本上可以隨意布局。如下圖所示
代碼是 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><Buttonandroid:id="@+id/button1"style="?android:attr/buttonStyleSmall"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:text="Button" /><Buttonandroid:id="@+id/button2"style="?android:attr/buttonStyleSmall"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBaseline="@+id/button1"android:layout_alignBottom="@+id/button1"android:layout_alignParentRight="true"android:layout_marginRight="14dp"android:text="Button" /><Buttonandroid:id="@+id/button3"style="?android:attr/buttonStyleSmall"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/button1"android:layout_centerHorizontal="true"android:layout_marginTop="97dp"android:text="Button" /><Buttonandroid:id="@+id/button4"style="?android:attr/buttonStyleSmall"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/button1"android:layout_below="@+id/button3"android:layout_marginTop="89dp"android:text="Button" /><Buttonandroid:id="@+id/button5"style="?android:attr/buttonStyleSmall"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/button2"android:layout_alignTop="@+id/button4"android:text="Button" /></RelativeLayout>
layout_marginBottom是指控件邊以外空下的距離,比如Button1和Button2垂直顯示,將Button1中layout_marginBottom = 10dp,那么Button1與Button2之間將有10dp距離。
下面這兩句是居左顯示和居右顯示

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

常用語句總結:

 android:layout_toLeftOf —— 該組件位于引用組件的左方

    android:layout_toRightOf —— 該組件位于引用組件的右方

    android:layout_above —— 該組件位于引用組件的上方

    android:layout_below —— 該組件位于引用組件的下方

???    android:layout_alignParentLeft —— 該組件是否對齊父組件的左端

???    android:layout_alignParentRight —— 該組件是否齊其父組件的右端

???    android:layout_alignParentTop —— 該組件是否對齊父組件的頂部

???    android:layout_alignParentBottom —— 該組件是否對齊父組件的底部

    android:layout_centerInParent —— 該組件是否相對于父組件居中
 

    android:layout_centerHorizontal —— 該組件是否橫向居中
   

  android:layout_centerVertical —— 該組件是否垂直居中

總之,相對視圖應該是最有用的,具體的操作比較復雜,更多的是通過圖形界面拖拉,再用代碼微調!

3.FrameLayout

這個布局很簡單,而且感覺有點二二的,哈哈!就是控件一個挨一個在左上角羅列。

代碼 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><Buttonandroid:id="@+id/button1"style="?android:attr/buttonStyleSmall"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button" /><Buttonandroid:id="@+id/button2"android:layout_width="126dp"android:layout_height="135dp"android:text="Button" /><Buttonandroid:id="@+id/button3"android:layout_width="194dp"android:layout_height="232dp"android:text="Button" /></FrameLayout>

4.絕對布局 AbsoluteLayout


絕對布局比較容易使用,就是以左上方為原點建立坐標系。每個控件用layout_x和layout_y表示位置。但是據說這種布局比較剛性,不容易適配各種終端,所以要慎用! 代碼 <?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_x="44dp"android:layout_y="18dp"android:text="Button" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_x="122dp"android:layout_y="173dp"android:text="Button" /><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_x="36dp"android:layout_y="133dp"android:text="Button" /></AbsoluteLayout>

5.表格布局 TableLayout

TableLayout有點像一個表格或是矩陣。在布局中加入TableRow,它的屬性是horizontal所以每個TableRow只能橫放。它里面的每個控件的高都是一樣的。下圖所示,是加入了一個TableRow和里面的控件。
代碼 <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><TableRowandroid:id="@+id/tableRow1"android:layout_width="wrap_content"android:layout_height="wrap_content" ><Buttonandroid:id="@+id/button1"style="?android:attr/buttonStyleSmall"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button" /><Buttonandroid:id="@+id/button2"style="?android:attr/buttonStyleSmall"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button" /><Buttonandroid:id="@+id/button3"style="?android:attr/buttonStyleSmall"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button" /><Buttonandroid:id="@+id/button4"style="?android:attr/buttonStyleSmall"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button" /><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView" /></TableRow></TableLayout>
? ok!你學會了么,have fun!

總結

以上是生活随笔為你收集整理的【android-tips】android xml布局总结篇的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 成人午夜福利一区二区 | 欧美视频在线观看免费 | 久久久久成人精品免费播放动漫 | 特黄aaaaaaaaa真人毛片 | 国产免费av网| av网站国产 | 手机av中文字幕 | 免费毛片网 | 美女色网站 | 日韩欧美二区 | 欧洲精品视频在线观看 | 亚洲成人v | www.人人干 | 日本少妇久久 | 无码人妻少妇色欲av一区二区 | 亚洲高清在线观看视频 | 欧洲a级片 | 西西44rtwww国产精品 | jizz视频| 丁香色综合 | 青青青青草 | 亚洲国产成人一区 | 91大神在线免费观看 | 精品久久久久久久久久久久久久久久久久 | 天天射日日 | 成人乱码一区二区三区av | 免费涩涩| 国产一区二区三区四 | aaa午夜 | 超碰2020| 久久亚洲婷婷 | 国产手机在线播放 | 亚洲最新av网站 | 天天躁日日躁狠狠躁欧美 | japanese av在线| 日本乱偷人妻中文字幕在线 | 欧美日韩视频免费观看 | 久91 | 999精品| 中文日韩字幕 | 日韩影视一区二区三区 | 日韩视频在线观看一区二区三区 | 国产在线视频你懂得 | 老熟妇毛茸茸 | 二区三区在线观看 | 日韩国产欧美精品 | 日本一本在线观看 | 色哟哟免费观看 | 日本视频在线观看免费 | av集中营 | 高清国产在线 | 九九色综合网 | 欧美在线亚洲 | www.爱色av.com| 国产一区欧美日韩 | 成人激情自拍 | 欧美日韩亚洲色图 | 欧美黑人一区二区三区 | 午夜少妇av| 最新av网站在线观看 | 中文一区二区在线播放 | 精品成人一区 | 日本午夜视频 | 狠色综合| 国产探花在线精品一区二区 | 久久国语精品 | 五月天激情综合 | 精品九九九九 | www.嫩草.com| 欧美日韩亚洲激情 | h网站免费在线观看 | 国产一级做a爱免费视频 | 麻豆爱爱| 欧美一二三| 亚洲视频精品在线 | 国产欧美精品一区 | 日日碰碰| 成人h动漫精品一区二 | 成人一级片 | 精品三级在线观看 | 国产露出视频 | 色婷婷精品国产一区二区三区 | 免费观看av毛片 | 成人91免费 | 中国一级特黄毛片大片 | 精品视频久久 | v天堂在线观看 | 一节黄色片 | 天天热天天干 | 欧美日韩四区 | 男人与雌性宠物交啪啪 | 黄黄视频在线观看 | 美女性生活视频 | www.伊人网 | 网站免费黄色 | 国产麻豆成人传媒免费观看 | 98超碰在线 | 日韩精品视频在线 | 69社|