Android動態(tài)加載布局實現(xiàn)技巧介紹
使用限定符
在平板上面大多數(shù)時候采用的雙頁的模式,程序會在左側(cè)列表上顯示一個包含子項列表,右側(cè)的面版會顯示詳細的內(nèi)容的因為平板具有足夠大的屏幕.完全能夠顯示兩頁的內(nèi)容.但是在手機上手機只能顯示一頁的內(nèi)容,因此需要兩個頁面分開顯示.
- 在運行時判斷程序應(yīng)該使用雙頁模式還是單頁模式,需要借助限定符==(qualifier)==來進行實現(xiàn).
- 在layout/activity_main.xml中只包含一個Fragment,即單頁模式
<?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="horizontal">
<fragment
android:id="@+id/leftFrag"
android:name="com.zb.fragmenttest.LeftFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>創(chuàng)建一個layout_large目錄,在這個目錄下創(chuàng)建一個同樣名為activity_main.xml的文件,但是在該布局當(dāng)中包含兩個Fragment,即雙頁模式.
<?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="horizontal">
<fragment
android:id="@+id/leftFrag"
android:name="com.zb.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/rightFrag"
android:name="com.zb.fragmenttest.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>- 解決在Android開發(fā)中l(wèi)ayout_large目錄下不能創(chuàng)建xml文件的方法:https://blog.csdn.net/CEVERY/article/details/86593814
- 其中l(wèi)arge就是一個限定符,那些屏幕被認為是large的設(shè)備就睡加載layout_large文件夾下的布局,小屏幕設(shè)備則還是會加載layout文件夾下面的布局.
- 這樣就可以實現(xiàn)動態(tài)加載布局的功能.
- 安卓中常見的限定符

使用最小寬度限定符
- 最小寬度限定符,允許我們對屏幕的寬度指定一個最小值(以dp為單位)
- 然后以這個最小值為臨界點.屏幕寬度大于這個值得設(shè)備就加載一個布局
- 屏幕寬度小于這個值得就加載另外一個布局
- 在res目錄下新家一個layout-sw600dp文件夾,然后在這個文件夾下面建一個activity_main.xml布局
<?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="horizontal">
<fragment
android:id="@+id/leftFrag"
android:name="com.zb.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:name="com.zb.fragmenttest.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
這就意味著,當(dāng)程序運行在屏幕寬度大于等于600dp的設(shè)備上時,會加載layout_sw600dp/activity_main布局,當(dāng)程序運行在屏幕寬度小于600dp的設(shè)備上的時候,則仍然加載默認的layout/activity_main布局.
到此這篇關(guān)于Android動態(tài)加載布局實現(xiàn)技巧介紹的文章就介紹到這了,更多相關(guān)Android動態(tài)加載布局內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android 應(yīng)用的歡迎界面實現(xiàn)代碼
本文主要介紹Android 應(yīng)用歡迎界面的開發(fā),這里提供實現(xiàn)方法和實現(xiàn)代碼以供大家參考,有需要的朋友可以參考下2016-07-07
探究Android客戶端網(wǎng)絡(luò)預(yù)連接優(yōu)化機制
一般情況下,我們都是用一些封裝好的網(wǎng)絡(luò)框架去請求網(wǎng)絡(luò),對底層實現(xiàn)不甚關(guān)注,而大部分情況下也不需要特別關(guān)注處理。了解底層的一些實現(xiàn),有益于我們對網(wǎng)絡(luò)加載進行優(yōu)化。本文就是關(guān)于根據(jù)http的連接復(fù)用機制來優(yōu)化網(wǎng)絡(luò)加載速度的原理與細節(jié)2021-06-06
Android使用AsyncQueryHandler實現(xiàn)獲取手機聯(lián)系人功能
這篇文章主要為大家詳細介紹了Android使用AsyncQueryHandler實現(xiàn)獲取手機聯(lián)系人功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
淺談 Android 7.0 多窗口分屏模式的實現(xiàn)
這篇文章主要介紹了淺談 Android 7.0 多窗口分屏模式的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
詳解Android中的MVP架構(gòu)分解和實現(xiàn)
本篇文章主要介紹了詳解Android中的MVP架構(gòu)分解和實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02

