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

FrameLayout(幀布局),她沒有方便的定位方式,所有的控件都會默認擺放在布局的左上角。

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

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"> <TextViewandroid:id="@+id/text_view"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="This is TextView"/><ImageViewandroid:id="@+id/image_view"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/ic_launcher"/>
</FrameLayout>

運行程序之后,可以看到文字和圖片都是位于布局的左上角,由于ImageView是在TextView之后添加的因此圖片壓在了文字的上面。這是一種默認效果,除了這種情況之外,我們還可以使用layout_gravity屬性來指定控件在布局中的對齊方式,這和LinearLayout中的用法是相似的,修改activity_main.xml中的代碼,如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"><TextViewandroid:id="@+id/text_view"android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_gravity="left"android:text="This is TextView"/><ImageViewandroid:id="@+id/image_view"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="right"android:src="@mipmap/ic_launcher"/></FrameLayout>

總的來說,FrameLayout定位方式欠缺,所以應用場景也少。


  • 百分百布局

可以發現只有LinearLayout支持使用layout_weight屬性來實現按比例指定控件大小的功能,其他兩種不支持。

百分百布局中,我們不再使用wrap_content、match_parent等方式來指定控件的大小,而是允許直接指定控件在布局中所占的百分比,這樣的話就可以輕松實現評分布局甚至是任意比例分割布局的效果了。

LinearLayout本身已經支持按比例指定控件的大小了,因此百分比布局只為了FrameLayout和RelativeLayout進行了功能擴展,提供了PercentFrameLayout和PercentRelative這兩個全新的布局。

不同于前三種,百分比布局屬于新增布局,Android 團隊將百分比布局定義在了support庫當中,我們只需要在項目的build。gradle中添加百分比布局庫的依賴。打開app/build.gradle文件,在dependencies閉包中添加了如下內容:

dependencies {implementation fileTree(dir: 'libs', include: ['*.jar'])implementation 'com.android.support:appcompat-v7:26.1.0'compile 'com.android.support:percent:24.2.1'implementation 'com.android.support.constraint:constraint-layout:1.0.2'testImplementation 'junit:junit:4.12'androidTestImplementation 'com.android.support.test:runner:1.0.1'androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

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


<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentFrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"> <Buttonandroid:id="@+id/button1"android:text="Button 1"android:layout_gravity="left|top"app:layout_widthPercent = "%50"app:layout_heightPercent = "%50"/><Buttonandroid:id="@+id/button2"android:text="Button 2"android:layout_gravity="right|top"app:layout_widthPercent = "%50"app:layout_heightPercent = "%50"/><Buttonandroid:id="@+id/button3"android:text="Button 3"android:layout_gravity="left|bottom"app:layout_widthPercent = "%50"app:layout_heightPercent = "%50"/><Buttonandroid:id="@+id/button4"android:text="Button 4"android:layout_gravity="left|bottom"app:layout_widthPercent = "%50"app:layout_heightPercent = "%50"/></android.support.percent.PercentFrameLayout>

最外層我們使用了PercentFrameLayout,由于百分比布局并不是內置在系統SDK當中的,所有需要把完整的包路徑寫出來。然后還必須定義一個app的命名空間,這樣才能使用 百分比布局的自定義屬性。

在PercentFrameLayout中我們定義了4個按鈕,使用app:layout_widthPercent屬性將各按鈕的寬度指定為布局的50%,

使用app:layout_heightPercent屬性將各按鈕的高度指定為布局的50%,不過PercentFrameLayout還是會繼承FrameLayout的特性,即所有的控件默認擺放在布局的左上角。為了不讓布局重疊,我們還是借助了layout_gravity來分別將這4個按鈕放置在布局的左上,右上,左下,右下4個位置。

運行程序如圖:

(待續。。。)

PercenFrameLayout就介紹到這里,還有一個PercentRelativeLayout的用法也是非常相似的。它繼承了RelativeLayout等布局。






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

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

相關文章

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方法來刪除對象的。這是比較簡…

頁面返回頂部(方法比較)

下面就說下簡單的返回頂部效果的代碼實現&#xff0c;附注釋說明。 1. 最簡單的靜態返回頂部&#xff0c;點擊直接跳轉頁面頂部&#xff0c;常見于固定放置在頁面底部返回頂部功能 方法一&#xff1a;用命名錨點擊返回到頂部預設的id為top的元素 html代碼 <a href"#top…

Android 第十六課 使用LitePal查詢數據

LitePal在查詢API方面做了非常多的優化&#xff0c;基本上可以滿足絕大多數場景的查詢需求&#xff0c;并且代碼也十分整潔。 例如我們需要查詢表中的所有數據&#xff1a; List<books> DataSupport.findAll(Book.class); 沒有冗長的參數列表&#xff0c;只需要調用一下…

linux創建桌面圖標,和開始菜單欄圖標

轉自&#xff1a;http://blog.csdn.net/qq_25773973/article/details/50514767 ###環境&#xff1a;Mint17&#xff0c;&#xff08;其他類似的linux系統是一樣的&#xff09; 如果開始菜單有圖標&#xff0c;創建桌面圖標很簡單&#xff0c;右鍵添加到桌面即可。 如果沒有&am…