国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

4種Android屏幕自適應(yīng)解決方案

2019-12-12 05:01:42
字體:
供稿:網(wǎng)友

Android支持多屏幕機(jī)制即用為當(dāng)前設(shè)備屏幕提供一種合適的方式來共同管理并解析應(yīng)用資源。本文就介紹了4中Android屏幕自適應(yīng)解決方案。

一、細(xì)說layout_weight

目前最為推薦的Android多屏幕自適應(yīng)解決方案。

該屬性的作用是決定控件在其父布局中的顯示權(quán)重,一般用于線性布局中。其值越小,則對(duì)應(yīng)的layout_width或layout_height的優(yōu)先級(jí)就越高,一般橫向布局中,決定的是layout_width的優(yōu)先級(jí);縱向布局中,決定的是layout_height的優(yōu)先級(jí)。

傳統(tǒng)的layout_weight使用方法是將當(dāng)前控件的layout_width和layout_height都設(shè)置成fill_parent,這樣就可以把控件的顯示比例完全交給layout_weight;這樣使用的話,就出現(xiàn)了layout_weight越小,顯示比例越大的情況。不過對(duì)于2個(gè)控件還好,如果控件過多,且顯示比例也不相同的時(shí)候,控制起來就比較麻煩了,畢竟反比不是那么好確定的。

于是就有了現(xiàn)在最為流行的0px設(shè)值法。看似讓人難以理解的layout_height=0px的寫法,結(jié)合layout_weight,卻可以使控件成正比例顯示,輕松解決了當(dāng)前Android開發(fā)最為頭疼的碎片化問題之一。

先看下面的styles(style_layout.xml)

<?xml version="1.0" encoding="utf-8"?><resources> <!-- 全屏幕拉伸--> <style name="layout_full">   <item name="android:layout_width">fill_parent</item>   <item name="android:layout_height">fill_parent</item>  </style> <!-- 固定自身大小--> <style name="layout_wrap">   <item name="android:layout_width">wrap_content</item>   <item name="android:layout_height">wrap_content</item>  </style><!-- 橫向分布--> <style name="layout_horizontal" parent="layout_full">   <item name="android:layout_width">0px</item>  </style> <!-- 縱向分布--> <style name="layout_vertical" parent="layout_full">   <item name="android:layout_height">0px</item>  </style>     </resources> 

可以看到,layout_width和layout_height兩個(gè)屬性被我封裝成了4個(gè)style

根據(jù)實(shí)際布局情況,選用當(dāng)中的一種,不需要自己設(shè)置,看過我前一個(gè)ActivityGroup的Demo的同學(xué)應(yīng)該非常熟悉了

然后我的Demo的布局如下(weight_layout.xml)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    style="@style/layout_full"    android:orientation="vertical">    <LinearLayout         style="@style/layout_vertical"        android:layout_weight="1"        android:orientation="horizontal">         <View             style="@style/layout_horizontal"             android:background="#aa0000"             android:layout_weight="1"/>         <View             style="@style/layout_horizontal"             android:background="#00aa00"             android:layout_weight="4"/>         <View             style="@style/layout_horizontal"             android:background="#0000aa"             android:layout_weight="3"/>         <View             style="@style/layout_horizontal"             android:background="#aaaaaa"             android:layout_weight="2"/>             </LinearLayout>     <LinearLayout         style="@style/layout_vertical"        android:layout_weight="2"        android:orientation="vertical">        <View             style="@style/layout_vertical"             android:background="#ffffff"             android:layout_weight="4"/>             <View             style="@style/layout_vertical"             android:background="#aa0000"             android:layout_weight="3"/>         <View             style="@style/layout_vertical"             android:background="#00aa00"             android:layout_weight="2"/>         <View             style="@style/layout_vertical"             android:background="#0000aa"             android:layout_weight="1"/>     </LinearLayout></LinearLayout>

二、自定義尺寸法

這個(gè)是我自己想出來的方法,可能是個(gè)比較笨的方法,所以沒有多少人提過用這種方法解決自適應(yīng)的問題。雖然這個(gè)方法缺點(diǎn)也很多,但有時(shí)候也是個(gè)不錯(cuò)的方法。

可以看到我定義了兩套尺寸文件,我們可以看下其中一個(gè)文件

<?xml version="1.0" encoding="utf-8"?><resources><dimen name="height_1_80">6px</dimen><dimen name="height_2_80">12px</dimen><dimen name="height_3_80">18px</dimen><dimen name="height_4_80">24px</dimen><dimen name="height_5_80">30px</dimen><dimen name="height_6_80">36px</dimen><dimen name="height_7_80">42px</dimen><dimen name="height_8_80">48px</dimen><dimen name="height_9_80">54px</dimen><dimen name="height_10_80">60px</dimen><dimen name="height_11_80">66px</dimen><dimen name="height_12_80">72px</dimen><dimen name="height_13_80">78px</dimen><dimen name="height_14_80">84px</dimen><dimen name="height_15_80">90px</dimen><dimen name="height_16_80">96px</dimen><dimen name="height_17_80">102px</dimen><dimen name="height_18_80">108px</dimen><dimen name="height_19_80">114px</dimen><dimen name="height_20_80">120px</dimen><dimen name="height_21_80">126px</dimen><dimen name="height_22_80">132px</dimen><dimen name="height_23_80">138px</dimen><dimen name="height_24_80">144px</dimen><dimen name="height_25_80">150px</dimen><dimen name="height_26_80">156px</dimen><dimen name="height_27_80">162px</dimen><dimen name="height_28_80">168px</dimen><dimen name="height_29_80">174px</dimen><dimen name="height_30_80">180px</dimen><dimen name="height_31_80">186px</dimen><dimen name="height_32_80">192px</dimen><dimen name="height_33_80">198px</dimen><dimen name="height_34_80">204px</dimen><dimen name="height_35_80">210px</dimen><dimen name="height_36_80">216px</dimen><dimen name="height_37_80">222px</dimen><dimen name="height_38_80">228px</dimen><dimen name="height_39_80">234px</dimen><dimen name="height_40_80">240px</dimen><dimen name="height_41_80">246px</dimen><dimen name="height_42_80">252px</dimen><dimen name="height_43_80">258px</dimen><dimen name="height_44_80">264px</dimen><dimen name="height_45_80">270px</dimen><dimen name="height_46_80">276px</dimen><dimen name="height_47_80">282px</dimen><dimen name="height_48_80">288px</dimen><dimen name="height_49_80">294px</dimen><dimen name="height_50_80">300px</dimen><dimen name="height_51_80">306px</dimen><dimen name="height_52_80">312px</dimen><dimen name="height_53_80">318px</dimen><dimen name="height_54_80">324px</dimen><dimen name="height_55_80">330px</dimen><dimen name="height_56_80">336px</dimen><dimen name="height_57_80">342px</dimen><dimen name="height_58_80">348px</dimen><dimen name="height_59_80">354px</dimen><dimen name="height_60_80">360px</dimen><dimen name="height_61_80">366px</dimen><dimen name="height_62_80">372px</dimen><dimen name="height_63_80">378px</dimen><dimen name="height_64_80">384px</dimen><dimen name="height_65_80">390px</dimen><dimen name="height_66_80">396px</dimen><dimen name="height_67_80">402px</dimen><dimen name="height_68_80">408px</dimen><dimen name="height_69_80">414px</dimen><dimen name="height_70_80">420px</dimen><dimen name="height_71_80">426px</dimen><dimen name="height_72_80">432px</dimen><dimen name="height_73_80">438px</dimen><dimen name="height_74_80">444px</dimen><dimen name="height_75_80">450px</dimen><dimen name="height_76_80">456px</dimen><dimen name="height_77_80">462px</dimen><dimen name="height_78_80">468px</dimen><dimen name="height_79_80">474px</dimen><dimen name="height_80_80">480px</dimen>  </resources>

這個(gè)是values-480x320文件夾下dimens_height.xml文件中的代碼,我把整個(gè)高度分成了80等分,這是因?yàn)榇蟛糠制聊坏膶挾然蚋叨榷际?0的整數(shù)倍(個(gè)別特殊的除外),不同的等分在不同的分辨率中設(shè)定不同的尺寸值。

由于每一套界面都要寫一套,所以有些同學(xué)可能覺著不太好,不過這個(gè)寫起來比較簡(jiǎn)單,而且以后也不用改,所以有時(shí)候也可以考慮用一下!

再看我Demo的布局代碼(dimen_layout.xml)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"     android:layout_height="fill_parent"    android:orientation="vertical">         <View             android:layout_width="@dimen/width_76_80"             android:layout_height="@dimen/height_10_80"             android:background="#ffcccc"             android:layout_margin="@dimen/width_2_80"/>        <LinearLayout         android:layout_width="fill_parent"         android:layout_height="fill_parent">         <View             android:layout_width="@dimen/width_30_80"             android:layout_height="@dimen/height_50_80"             android:background="#ccccff"             android:layout_margin="@dimen/height_5_80"/>         <LinearLayout             android:layout_width="fill_parent"             android:layout_height="fill_parent"             android:orientation="vertical">                 <Button                 android:layout_width="@dimen/width_30_80"                 android:layout_height="@dimen/height_5_80"                 android:background="#ccffcc"                 android:layout_marginBottom="@dimen/height_1_80"                 android:text="5"/>             <Button                 android:layout_width="@dimen/width_30_80"                 android:layout_height="@dimen/height_10_80"                 android:background="#ccffcc"                 android:layout_marginBottom="@dimen/height_1_80"                 android:text="10"/>             <Button                 android:layout_width="@dimen/width_30_80"                 android:layout_height="@dimen/height_15_80"                 android:background="#ccffcc"                 android:layout_marginBottom="@dimen/height_1_80"                 android:text="15"/>             <Button                 android:layout_width="@dimen/width_30_80"                 android:layout_height="@dimen/height_20_80"                 android:background="#ccffcc"                 android:text="20"/>         </LinearLayout>         </LinearLayout>        </LinearLayout>

以上是我寫的統(tǒng)一的布局代碼,大家注意我的代碼中有margin這樣的值也用到了自定義尺寸,如果這個(gè)margin使用layout_weight來控制的話,無疑要多嵌套一層線性布局,所以說沒有一個(gè)方法是十全十美的,這第2個(gè)方法有時(shí)候用起來反而還要方便一些。。

三、在java代碼中設(shè)置寬高度

也許很多人會(huì)反對(duì)這種方法,因?yàn)榧词故枪俜揭彩峭扑]使用xml的方式寫布局。不過我們?cè)谶@不會(huì)像Swing那樣寫那么多麻煩的布局代碼,因?yàn)槲覀冎皇窃诖a中重新設(shè)定控件的寬高度而已,其他屬性依然是交給xml布局文件的。這個(gè)方法其實(shí)是我跟同事偷學(xué)來的,雖然我不贊成這樣的方法,但他確確實(shí)實(shí)也是解決屏幕自適應(yīng)問題的方案之一,而且它沒我想象的那么復(fù)雜,其實(shí)很簡(jiǎn)單。

首先我們要做的是獲取當(dāng)前屏幕的寬高度,因?yàn)檫@個(gè)在后面要用到

我們可以寫兩個(gè)靜態(tài)變量用來保存當(dāng)前屏幕的寬高度:

public class Constant {    public static int displayWidth; //屏幕寬度    public static int displayHeight; //屏幕高度}

然后在第一個(gè)Activity啟動(dòng)的時(shí)候,獲取這兩個(gè)值

DisplayMetrics displayMetrics = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);Constant.displayWidth = displayMetrics.widthPixels;Constant.displayHeight = displayMetrics.heightPixels;

布局代碼我們可以全都統(tǒng)一寫成wrap-content,其實(shí)寫成什么都無所謂,因?yàn)檫@個(gè)值只是暫時(shí)的

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent"><Button     android:id="@+id/btn1"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:background="#ffcccc"  android:text="aaaaaaaa"/><Button     android:id="@+id/btn2"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:background="#ccffcc"  android:text="bbbbbbbbb"/><Button     android:id="@+id/btn3"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:background="#ccccff"  android:text="ccccccccc"/><Button     android:id="@+id/btn4"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:background="#ffffcc"  android:text="dddddddddddddddddd"/>  </LinearLayout>

最后我們?cè)贏ctivity的onCreate方法里這么做

// 第一個(gè)按鈕,寬度100%,高度10%        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(                LayoutParams.FILL_PARENT,                (int) (Constant.displayHeight * 0.1f + 0.5f));        btn1.setLayoutParams(params);        // 第二個(gè)按鈕,寬度100%,高度30%        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(                LayoutParams.FILL_PARENT,                (int) (Constant.displayHeight * 0.3f + 0.5f));        btn2.setLayoutParams(params2);        // 第三個(gè)按鈕,寬度50%,高度20%        LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(                (int) (Constant.displayWidth * 0.5f + 0.5f),                (int) (Constant.displayHeight * 0.2f + 0.5f));        btn3.setLayoutParams(params3);        // 第三個(gè)按鈕,寬度70%,高度填滿剩下的空間        LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(                (int) (Constant.displayWidth * 0.7f + 0.5f),                LayoutParams.FILL_PARENT);        btn4.setLayoutParams(params4);

四、多布局

做為最后的方法,也是最后一個(gè)才會(huì)考慮的方法,那就是為不同的尺寸界面單獨(dú)寫布局。不到萬不得已不要用這個(gè)方法,相信不少人和我一樣都被逼著用過這個(gè)方法吧。需要說明的是,橫豎屏切換使用不同布局也是用這個(gè)方法解決的;代碼我就不上.

補(bǔ)充一下,寫多個(gè)布局的時(shí)候,配置文件一定要加上這段配置代碼,不然有時(shí)可能會(huì)出問題

<supports-screens Android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" />

五、其他

以上說的都是多個(gè)屏幕顯示相同內(nèi)容需要考慮的問題,還有一種是在不同的屏幕上顯示內(nèi)容不同的情況,其實(shí)這個(gè)問題我們往往是用滾動(dòng)視圖來解決的,也就是ScrowView;需要注意的是ScrowView中使用layout_weight是無效的,既然使用ScrowView了,就把它里面的控件的大小都設(shè)成固定的吧。

此外關(guān)于圖片的自適應(yīng)問題,主要是2點(diǎn),一個(gè)是9patch圖,這個(gè)東西大家都要學(xué)會(huì)去做,不難;不過有些編譯器在識(shí)別9patch圖時(shí)會(huì)出這樣那樣的bug,像我的Eclipse就不認(rèn)這個(gè),而同一個(gè)9patch圖在別的電腦上卻是沒問題的,

第二個(gè)要說的是我曾經(jīng)被困擾的一個(gè)問題,對(duì)于480x800 和 480x854這兩個(gè)尺寸,他們顯示同一個(gè)圖片時(shí),總有一個(gè)會(huì)拉伸(如果9patch可以解決的還好)。其實(shí)當(dāng)初困擾我的是,這兩個(gè)尺寸都是hdpi的,以為無法給這兩個(gè)屏幕做不同的圖片。后來無意中發(fā)現(xiàn),圖片可以和布局一樣分多個(gè)尺寸的,而不僅僅是根據(jù)密度分,也就是說你可以寫這樣的文件夾drawable-hdpi-800x480和drawable-hdpi-854x480,在它們里面放不同的圖片,這樣圖片也能自適應(yīng)了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 耿马| 三亚市| 长武县| 赫章县| 花莲县| 陈巴尔虎旗| 桑日县| 平潭县| 驻马店市| 宁远县| 荆州市| 龙泉市| 通州区| 且末县| 阆中市| 海宁市| 尼勒克县| 深圳市| 乐亭县| 枝江市| 江口县| 建阳市| 普定县| 阿尔山市| 江永县| 宜都市| 新郑市| 平果县| 密云县| 台东市| 张掖市| 积石山| 工布江达县| 崇明县| 拜泉县| 绥滨县| 镇巴县| 龙门县| 贵港市| 嘉义县| 鄂托克前旗|