Android 第六課 4種基本布局之LinearLayout和Relativelayout

看完控件,緊接著看布局,布局是可以來放置控件,管理控件的。布局里也可以嵌套布局。


我們新建項目UILayoutTest項目,活動名和布局名選擇默認。加入活動及其對應的布局已經創建完成。

  • 線性布局(LinearLayout)

  • android:layout_gravity屬性
  • android:layout_weight屬性

  • 相對布局(RelativeLayout)

  • 相對于父布局定位
  • 相對于控件定位:注意:當一個控件去引用另一個控件的id時,該控件一定要定義在引用控件的后面,不然會找不到id的情況

????????LinearLayout布局會將它所包含的控件在線性方向上一次排列,所謂線性方向,可能是垂直方向,或者是水平方向,前面我們都是在垂直方向上排列控件,我們可以通過android:orientation屬性指定了排列方向是vertical,如果指定的是horizontal,控件就會在水平方向上排列了。修改activity_layout.xml新增加三個按鈕,我們可以想不寫android:orientation屬性,發現3個按鈕根據自身大小水平排列,因為這是LinearLayout布局默認的控件排列方式,(倘若你的第一個控件的android:layout_width="match_parent",就是說你的一個控件的寬度等于整個屏幕的寬度,那么你后面的哪個控件很有可能顯示不出來。)

等到我們寫過android:orientation="vertical",我們會發現3個按鈕垂直的排列了。

????? ??

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.uilayouttest.MainActivity"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 1"/><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 2"/><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 3"android:textAllCaps="false"/></LinearLayout>

這里需要注意:如果LinearLayout的排列方式是horizontal,內部的控件就不能將寬度指定為match_parent,因為這樣,單獨一個控件會將整個水平方向占滿,其他控件就沒有可以放置的位置了。同樣的道理如果LinearLayout的排列方式是horizontal,內部控件就不能將高度指定為match_parent,因為這樣,單獨一個控件會將整個垂直方向占滿。

我們首先來看布局的android:layout_gravity屬性,我們之前看過android:gravity屬性,這兩者有些相似。區別是:android:gravity是指定文字在控件中的對齊方式,而android:layout_gravity是指定控件在布局中的對齊方式。兩者的可選值差不多,但是需要注意:LinearLayout的排列方向是horizontal時,只有垂直方向上的對齊方式才會生效,因為此時水平方向的長度是不固定的,每增加一個控件,水平方向上的長度都會改變,因而無法指定該方向上的對齊方式。同樣道理,當LinearLayout的排列方向是vertical時,只有水平方向上的對齊方式才會生效。修改activity_main.xml文件,代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.uilayouttest.MainActivity"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="top"android:text="Button 1"/><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:text="Button 2"/><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom"android:text="Button 3"android:textAllCaps="false"/></LinearLayout>

我們再來看LineraLayout中另一個重要屬性android:layout_weight。它允許我們使用比例的方式來指定控件的大小,它可以去適應手機的屏幕。下面我們來編輯一個消息發現界面,需要一個文本編輯框和一個發送按鈕,修改activity_main.xml中的代碼,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.uilayouttest.MainActivity"><EditTextandroid:id="@+id/input_message"android:layout_width="0dp"android:layout_height="wrap_content" android:layout_weight="3"android:hint="Type something"/><Buttonandroid:id="@+id/send"android:layout_width="0dp"android:layout_height="wrap_content" android:layout_weight="2"android:text="Send"/></LinearLayout>

發現<EditText>和<Button>的寬度都指定為了0dp,不過你不用擔心文本編輯框和按鈕不存在,因為我們又接著使用了android:layout_weight屬性,此時控件大小由android:layout_weight來決定。這里0dp是一種比較規范的寫法。另外,dp是Android中用于指定控件大小、間距等屬性的單位。在這里我們把<EditText>和<Button>的android:layou_weight分別設置為3和2,這表明EditText占整個屏幕寬度的3份,Button占2份(我們是將整個屏幕分為3+2份)。

當然,我們也可以只是指定部分控件的layout_weight值來實現更好的效果。修改activity_main.xml文件中的代碼,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.uilayouttest.MainActivity"><EditTextandroid:id="@+id/input_message"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="3"android:hint="Type something"/><Buttonandroid:id="@+id/send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Send"/></LinearLayout>

我們只是指定了EditText的android:layout_weight屬性,并將Button的寬度改為wrap_content。這表明Button的寬度仍然按照wrap_content來計算,而EditText則會占滿屏幕的所有剩余空間。




  • 相對布局(RelativeLayout)
  • android:layout_alignParentLeft屬性可以讓控件和父布局的左上角對齊
  • android:layout_above屬性可以讓一個控件位于另一個控件的上方
  • android:layout_alignLeft表示讓一個控件的左邊緣和另一個控件的左邊緣對齊

相比較LinearLayout,RelativeLayout更加隨意一些,它通過相對定位的方式讓控件出現在布局的任何位置。正因為如此,RelativeLayout的屬性非常多,不過都是有規律可循的。修改activity_main.xml中的代碼,如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.uilayouttest.MainActivity"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:text="button 1"/><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_alignParentTop="true"android:text="button 2"/><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="button 3"/><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentBottom="true"android:text="button 4"/><Buttonandroid:id="@+id/button5"android:textAllCaps="false"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:text="button 5"/></RelativeLayout>

我們讓Button 1和父布局的左上角對齊,Button 2和父布局的右上角對齊,Button 3居中顯示,Button 4 和父布局的左下角對齊,Button 5 和父布局的右下角對齊。程序運行如下:



上面這是相對于父布局定位。

下面我們相對于控件進行定位。

修改activity_main.xml中的代碼,如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.uilayouttest.MainActivity"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_above="@id/button3"android:layout_toLeftOf="@id/button3"android:text="button 1"/><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@id/button3"android:layout_toRightOf="@id/button3"android:text="button 2"/><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="button 3"/><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/button3"android:layout_toLeftOf="@id/button3"android:text="button 4"/><Buttonandroid:id="@+id/button5"android:textAllCaps="false"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/button3"android:layout_toRightOf="@id/button3"android:text="button 5"/></RelativeLayout>

其中android:layout_above屬性可以讓一個控件位于另一個控件的上方,需要為這個屬性指定相對控件的id的引用。

android:layout_toLeftOf表示一個控件位于另一個控件的左側。注意:當一個控件去引用另一個控件的id時,該控件一定要定義在引用控件的后面,不然會找不到id的情況。重新運行程序,如下圖:


RelativeLayout中還有另外一組相對于控件進行定位的屬性,android:layout_alignLeft表示讓一個控件的左邊緣和另一個控件的左邊緣對齊,android:layout_alignRight、android:layout_alignTop、android:layout_alignBottom道理是一樣的。



本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274125.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274125.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274125.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

如何在UI設計中制作完美陰影

重點 (Top highlight)Shadows are everywhere in modern UI Designs. They are one of the most essential part of the UI elements right behind the fill, stroke, and cornder radius. &#x1f609;現代UI設計中到處都有陰影。 它們是UI元素中最重要的部分之一&#xff0c…

微軟2013年校園實習生招聘筆試題及答案

原文&#xff1a; http://www.wangkaimin.com/2013/04/07/%e5%be%ae%e8%bd%af2013%e5%b9%b4%e6%a0%a1%e5%9b%ad%e5%ae%9e%e4%b9%a0%e7%94%9f%e6%8b%9b%e8%81%98%e7%ac%94%e8%af%95%e9%a2%98%e5%8f%8a%e7%ad%94%e6%a1%88/#more-195 1. Which of following calling convension(s)…

Android 第七課 4種基本布局之FrameLayout和百分比布局

FrameLayout&#xff08;幀布局&#xff09;&#xff0c;她沒有方便的定位方式&#xff0c;所有的控件都會默認擺放在布局的左上角。 修改activity_main.xml中的代碼&#xff0c;如下&#xff1a; <?xml version"1.0" encoding"utf-8"?> <Frame…

mongodb 群集圖_群集和重疊條形圖

mongodb 群集圖為什么和如何 (Why & How) 1.- Clustered Bar Charts1.- 集群條形圖 AKA: grouped, side-by-side, multiset [bar charts, bar graphs, column charts]AKA &#xff1a;分組&#xff0c;并排&#xff0c;多組[條形圖&#xff0c;條形圖&#xff0c;柱形圖] …

第一次寫python

這是一個在BJDP上學習Coding Kata的時候用到的一個練習&#xff0c;原來打算用Java寫的&#xff0c;但是一想正好是學習的好機會。 就用Python了。第一次&#xff0c;寫的有些復雜。 這個題目是關于購買圖書的打折信息的。 題目來源&#xff1a; http://codingdojo.org/cgi-bin…

Android 第八課 創建自定義控件

常用控件和布局的繼承結構&#xff0c;如下圖&#xff1a; &#xff08;待續。。。。&#xff09; 所有的控件都是直接或間接繼承自View的&#xff0c;所用的所有布局都是直接或間接繼承自ViewGroup的&#xff0c;View是Android中最基本的一種UI組件&#xff0c;它可以在屏幕上…

figma下載_搬到Figma對我意味著什么

figma下載A couple of years ago, amidst the boom of new design and prototyping software, I was pretty reluctant to fight on the Figma/Sketch cold war. I was working on a relatively small design team and, after years helping to design products, well sold on …

解決IE中img.onload失效的方法

解決IE中img.onload失效的方法 - CoffeeCats IT Blog - IT博客http://www.cnitblog.com/CoffeeCat/archive/2008/02/01/39533.htmlFirefox、Google Chrome不存在問題&#xff01;為什么onload沒有被IE調用呢&#xff1f;因為IE會緩存圖片&#xff0c;第2次加載的圖片&#xff0…

Android 第九課 常用控件-------ListView

ListView允許用戶通過手指上下滑動的方式將屏幕外的數據滾動到屏幕內&#xff0c;同時屏幕上原有數據將會滾動出屏幕。 1、ListView簡單用法 如何將ListView將你要顯示的大量內容關聯起來呢&#xff1f;這是個很重要的問題。 1、首先我們必須先將數據提供好&#xff0c;因為你的…

Singleton patterns 單件(創建型模式)

1、模式分類 1.1 從目的來看&#xff1a; ? – 創建型&#xff08;Creational&#xff09;模式&#xff1a;負責對象創建。 ? – 結構型&#xff08;Structural&#xff09;模式&#xff1a;處理類與對象間的組合。 ? – 行為型&#xff08;Behavioral&…

Android 第十一課 SQlite 數據庫存儲

Android 為了讓我們能夠更加方便的管理數據庫&#xff0c;特意提供了一個SQLiteOpenHelper幫助類&#xff0c;通過借助這個類就可以非常簡單的對數據庫進行創建和升級。 SQLiteOpenHelper是一個抽象類&#xff0c;我們要創建一個自己的幫助類去繼承它。SQLiteOpenHelper有兩個抽…

淺析SQL Server 2005中的主動式通知機制

一、引言 在開發多人同時訪問的Web應用程序&#xff08;其實不只這類程序&#xff09;時&#xff0c;開發人員往往會在緩存策略的設計上狠下功夫。這是因為&#xff0c;如果將這種環境下不常變更的數據臨時存放在應用程序服務器或是用戶機器上的話&#xff0c;可以避免頻繁地往…

Android 第十二課 使用LitePal操作數據庫(記得閱讀最后面的注意事項哦)

一、LitePal簡介 1、(新建項目LitePalTest)正式接觸第一個開源庫---LitePalLitePal是一款開源的Android 數據庫框架&#xff0c;它采用了對象關系映射&#xff08;ORM&#xff09;的模式。2、配置LitePal&#xff0c;編輯app/build.gradle文件&#xff0c;在dependencies閉包中…

listview頻繁刷新報錯

在Android編程中使用Adapter時&#xff0c;偶爾會出現如下錯誤&#xff1a;The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI t…

Android 第十三課 SharedPreferences存儲

SharedPreferences是使用鍵值對的方式來存儲數據的。當保存一條數據時&#xff0c;需要給這條數據提供一個對應的鍵&#xff0c;這樣在讀取數據的時候就可以通過這個鍵把相應的值取出來。而且支SharedPreferences還支持多種不同的數據類型存儲&#xff0c;例如&#xff1a;如果…

DSP的Gel作用

轉自&#xff1a;http://blog.csdn.net/azhgul/article/details/6660960最近剛在研究Davinci系&#xff0c;特此MARK下&#xff0c;以資后續學習之用。 DSP的Gel作用 1 GEL文件基本作用 當CCSStudio啟動時&#xff0c;GEL文件加載到PC機的內存中&#xff0c;如果定義了StartUp(…

解決關于登錄校園網顯示不在IP段的問題方案(要看注意事項哦!)

有時&#xff0c;登錄校園網&#xff0c;賬號和密碼都顯示正確&#xff0c;但是卻顯示出“賬號只能在指定IP段登錄”的問題。 那我們就提供了一個解決方案&#xff1a; 使用WinR,并在輸入框&#xff0c;輸入cmd命令&#xff1a;&#xff08;如下&#xff09;接著輸入&#xff1…

jquery插件編寫

jQuery為開發插件提拱了兩個方法&#xff0c;分別是&#xff1a; jQuery.fn.extend(object); jQuery.extend(object); jQuery.extend(object); 為擴展jQuery類本身.為類添加新的方法。可以理解為添加靜態方法。是全局的&#xff08;位于jQuery命名空間內部的函數&#xff09;…

gtk/Glade編程 編譯命令不成功 解決方法

摘自&#xff1a;http://blog.chinaunix.net/uid-26746982-id-3433656.html 當我們編寫gtk/glade程序&#xff0c;gcc編譯時&#xff0c;用如下命令&#xff1a; #gcc -o server server.c pkg-config --cflags --libs gtk-2.0 報錯&#xff1a;/tmp/ccoXadAd.o: In function …

Android 第十五課 如何使用LitePal從SQLite數據庫中刪除數據(十四課用來保留講解如何向SQLite數據庫中存入數據)

使用LitePal刪除數據的方式主要有兩種&#xff0c;第一種就是直接調用已存對象的delete()方法&#xff0c;所謂已存儲對象就是調用過save()方法的對象&#xff0c;或者說是通過LitePal提供的查詢API查出來的對象&#xff0c;都是可以直接使用delete方法來刪除對象的。這是比較簡…