這篇文章給大家介紹Android中怎么實(shí)現(xiàn)自適應(yīng)布局,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
成都創(chuàng)新互聯(lián)主營合浦網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP軟件開發(fā),合浦h5微信小程序搭建,合浦網(wǎng)站營銷推廣歡迎合浦等地區(qū)企業(yè)咨詢
res/values/styles.xml
<style name="Container"> <item name="android:layout_margin">0dp</item> <item name="android:padding">16dp</item> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">match_parent</item> <item name="android:orientation">vertical</item> <item name="android:background">@drawable/container_background</item> </style>
對于平板(豎直的)來說,我們可以添加一些外邊距,因?yàn)槠聊粔虼蟆?/p>
res/values-sw600dp/styles.xml
<style name="Container"> <item name="android:layout_margin">0dp</item> <item name="android:padding">32dp</item> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">match_parent</item> <item name="android:orientation">vertical</item> <item name="android:background">@drawable/container_background</item> </style>
在平板上豎直和水平的***區(qū)別在于,我們會加上margin值,這樣內(nèi)容就不會充滿整個屏幕了、我們還可以給父視圖加一個背景圖片,來填充空白區(qū)域。
res/values-sw600dp-land/styles.xml
<style name="Container"> <item name="android:layout_marginRight">130dp</item> <item name="android:layout_marginLeft">130dp</item> <item name="android:padding">32dp</item> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">match_parent</item> <item name="android:orientation">vertical</item> <item name="android:background">@drawable/container_background</item> </style>
然后我們可以在不同的屏幕上,這樣使用樣式文件:
<LinearLayout style="@style/Container"> ... buttons, edit texts, text views, etc ... </LinearLayout>
這是在四寸屏幕手機(jī)的效果:
這是在七寸平板上的效果:
這是在七寸平板的橫屏效果:
還有一些在CSS里面非常方便的屬性(比如bootstrap),它們是一些幫助類,如.visible-phone,.hidden-phone,.visible-tablet等等,在Android上也可以這么做。
<!-- Device Visibility --> <style name="PhoneOnly"> <item name="android:visibility">gone</item> </style> <style name="TabletOnly"> <item name="android:visibility">visible</item> </style> <style name="TabletPortraitOnly"> <item name="android:visibility">gone</item> </style> <style name="TabletLandscapeOnly"> <item name="android:visibility">visible</item> </style>
把這些樣式放在對應(yīng)的配置文件夾中,然后就可以在需要的時候隱藏顯示相應(yīng)的控件了。
<LinearLayout android:id="@+id/column_one"> ... some content ... </LinearLayout> <LinearLayout android:id="@+id/column_two" style="@style/TabletLandscapeOnly"> ... some extra content since we have space ... </LinearLayout>
對于平板(水平的)來說,此樣式會顯示兩列,但是對于大多數(shù)設(shè)備來說,第二列是不會顯示的。
僅僅用了幾行xml代碼,我們就能夠創(chuàng)建一個迷你的布局框架了,我們還可以擴(kuò)展這種技術(shù),根據(jù)需要去實(shí)現(xiàn)一個通用的樣式文件,以后可以用到項(xiàng)目中。
關(guān)于Android中怎么實(shí)現(xiàn)自適應(yīng)布局就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
分享題目:Android中怎么實(shí)現(xiàn)自適應(yīng)布局
文章地址:http://sd-ha.com/article14/jsjoge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、、動態(tài)網(wǎng)站、定制網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)