Android動(dòng)態(tài)加載布局實(shí)現(xiàn)技巧介紹
使用限定符
在平板上面大多數(shù)時(shí)候采用的雙頁(yè)的模式,程序會(huì)在左側(cè)列表上顯示一個(gè)包含子項(xiàng)列表,右側(cè)的面版會(huì)顯示詳細(xì)的內(nèi)容的因?yàn)槠桨寰哂凶銐虼蟮钠聊?完全能夠顯示兩頁(yè)的內(nèi)容.但是在手機(jī)上手機(jī)只能顯示一頁(yè)的內(nèi)容,因此需要兩個(gè)頁(yè)面分開(kāi)顯示.
- 在運(yùn)行時(shí)判斷程序應(yīng)該使用雙頁(yè)模式還是單頁(yè)模式,需要借助限定符==(qualifier)==來(lái)進(jìn)行實(shí)現(xiàn).
- 在layout/activity_main.xml中只包含一個(gè)Fragment,即單頁(yè)模式
<?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)建一個(gè)layout_large目錄,在這個(gè)目錄下創(chuàng)建一個(gè)同樣名為activity_main.xml的文件,但是在該布局當(dāng)中包含兩個(gè)Fragment,即雙頁(yè)模式.
<?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開(kāi)發(fā)中l(wèi)ayout_large目錄下不能創(chuàng)建xml文件的方法:https://blog.csdn.net/CEVERY/article/details/86593814
- 其中l(wèi)arge就是一個(gè)限定符,那些屏幕被認(rèn)為是large的設(shè)備就睡加載layout_large文件夾下的布局,小屏幕設(shè)備則還是會(huì)加載layout文件夾下面的布局.
- 這樣就可以實(shí)現(xiàn)動(dòng)態(tài)加載布局的功能.
- 安卓中常見(jiàn)的限定符
使用最小寬度限定符
- 最小寬度限定符,允許我們對(duì)屏幕的寬度指定一個(gè)最小值(以dp為單位)
- 然后以這個(gè)最小值為臨界點(diǎn).屏幕寬度大于這個(gè)值得設(shè)備就加載一個(gè)布局
- 屏幕寬度小于這個(gè)值得就加載另外一個(gè)布局
- 在res目錄下新家一個(gè)layout-sw600dp文件夾,然后在這個(gè)文件夾下面建一個(gè)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)程序運(yùn)行在屏幕寬度大于等于600dp的設(shè)備上時(shí),會(huì)加載layout_sw600dp/activity_main布局,當(dāng)程序運(yùn)行在屏幕寬度小于600dp的設(shè)備上的時(shí)候,則仍然加載默認(rèn)的layout/activity_main布局.
到此這篇關(guān)于Android動(dòng)態(tài)加載布局實(shí)現(xiàn)技巧介紹的文章就介紹到這了,更多相關(guān)Android動(dòng)態(tài)加載布局內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android convinientbanner頂部廣告輪播控件使用詳解
這篇文章主要為大家詳細(xì)介紹了Android convinientbanner頂部廣告輪播控件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android實(shí)現(xiàn)回彈ScrollView的原理
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)回彈ScrollView的原理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04AndroidImageSlider實(shí)現(xiàn)炫酷輪播廣告效果
這篇文章主要為大家詳細(xì)介紹了AndroidImageSlider實(shí)現(xiàn)炫酷輪播廣告效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08android在異步任務(wù)中關(guān)閉Cursor的代碼方法
android在異步任務(wù)中如何關(guān)閉Cursor?在我們開(kāi)發(fā)應(yīng)用的時(shí)候,很多時(shí)候會(huì)遇到這種問(wèn)題,下面我們就看看代碼如何實(shí)現(xiàn)2013-11-11Android設(shè)計(jì)模式之Builder模式詳解
這篇文章主要為大家詳細(xì)介紹了Android設(shè)計(jì)模式之Builder模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android之ImageSwitcher的實(shí)例詳解
這篇文章主要介紹了Android之ImageSwitcher的實(shí)例詳解的相關(guān)資料,這里提供實(shí)例幫助大家理解這個(gè)控件的功能,希望能幫助到大家,需要的朋友可以參考下2017-08-08Android定制RadioButton樣式三種實(shí)現(xiàn)方法
三種方法實(shí)現(xiàn)Android定制RadioButton樣式:使用XML文件進(jìn)行定義/在JAVA代碼中定義等等,感興趣的朋友可以參考下,希望可以幫助到你2013-02-02