天天看點

安卓螢幕适配問題1.權重适配2.圖檔适配3.dimens.xml檔案适配4.布局檔案适配5.java代碼适配 6.其他

1.權重适配

<span style="font-size:18px;">通過android提供的(權重)剩餘空間配置設定,以達到适配的效果
注意:這是控件的寬或者高的尺寸要設定為0dp 權重值越大所占比例越大
<span style="font-family:'Times New Roman';font-size:14px;color:#000000;border-collapse: separate; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-family:Helvetica, arial, freesans, clean, sans-serif;font-size:14px;color:#333333;line-height: 22px; "></span></span></span><pre style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; padding-top: 6px; padding-right: 10px; padding-bottom: 6px; padding-left: 10px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-style: initial; border-color: initial; font-size: 13px; font-family: Consolas, 'Liberation Mono', Courier, monospace; background-color: rgb(248, 248, 248); border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(204, 204, 204); border-right-color: rgb(204, 204, 204); border-bottom-color: rgb(204, 204, 204); border-left-color: rgb(204, 204, 204); line-height: 19px; overflow-x: auto; overflow-y: auto; border-top-left-radius: 3px 3px; border-top-right-radius: 3px 3px; border-bottom-right-radius: 3px 3px; border-bottom-left-radius: 3px 3px; "><code style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; font-size: 12px; font-family: Consolas, 'Liberation Mono', Courier, monospace; white-space: pre; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; background-color: transparent; border-top-left-radius: 3px 3px; border-top-right-radius: 3px 3px; border-bottom-right-radius: 3px 3px; border-bottom-left-radius: 3px 3px; border-width: initial; border-color: initial; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: initial; border-color: initial; background-position: initial initial; background-repeat: initial initial; ">   <TextView 
        android:background="#000000"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"/>
    <TextView 
        android:background="#123456"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"/></code>
           

如果被權重的尺寸設定為fill_parent就會出現一系列問題 是以要盡量規避

2.圖檔适配

安卓螢幕适配問題1.權重适配2.圖檔适配3.dimens.xml檔案适配4.布局檔案适配5.java代碼适配 6.其他
<span style="font-size:18px;"> 不同像素密度的手機加載工程資源檔案(res)中不同的資源圖檔</span>
           

3.dimens.xml檔案适配

<span style="font-size:18px;">    dimens.xml存在于工程資源(res)檔案夾中不同values(如:value-1280x720、value-800x480)檔案夾下,
可用于指定控件大小,不同像素密度手機加載不同values檔案夾下的dimens.xml檔案


</span>
           

4.布局檔案适配

<span style="font-size:18px;">不同分辨率的手機,加載不同的布局檔案已達到适配效果。建立多個layout(如:layout-1280x720、layout-800x480)
檔案夾用于存放不同像素密度手機所需布局檔案</span>
           

5.java代碼适配

通過android相應api擷取目前手機的寬高像素值,按比例配置設定螢幕中控件的寬高以達到适配效果

activity中oncreate核心代碼:
    TextView tv  = (TextView) findViewById(R.id.tv);
    //擷取封裝目前手機螢幕資訊對象,用于存放寬高值
    DisplayMetrics metrics  = new DisplayMetrics();
    //給目前螢幕設定寬高
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    //擷取高度
    Constant.srceenHeight = metrics.heightPixels;
    //擷取寬度
    Constant.srceenWidth = metrics.widthPixels;

    Log.i(tag, "Constant.srceenHeight = "+Constant.srceenHeight);
    Log.i(tag, "Constant.srceenWidth = "+Constant.srceenWidth);

    //寬高各占50%
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
            (int)(Constant.srceenWidth*0.5+0.5), 
            (int)(Constant.srceenHeight*0.5+0.5));
    tv.setLayoutParams(layoutParams);
           

6.其他

<span style="font-size:18px;">首先我會盡量使用線性布局,相對布局,如果螢幕放不下了,可以使用ScrollView(可以上下拖動)
ScrowView使用時要注意:
在不同的螢幕上顯示内容不同的情況,其實這個問題我們往往是用滾動視圖來解決的,也就是ScrowView;
需要注意的是<span style="color:#FF0000;">ScrowView中使用layout_weight是無效的</span>,既然使用ScrowView了,就把它裡面的控件的大小
都設成固定的吧。
 <strong>在指定寬高的時候,采用dip的機關,dp機關動态比對</strong>

</span><p>由于android<span style="color:#FF0000;"><strong>代碼中寫的機關都是像素</strong></span>,所有需要通過工具類進行轉化</p><p>盡量<strong>使用9-patch圖</strong>,可以自動的依據圖檔上面顯示的内容被拉伸和收縮。其中在編輯的時候,灰色區域是被拉伸的,</p><p>左上表示的是拉伸區域(可以用兩個點表示拉伸位置)  右下表示的是文本顯示區域
</p><p>工具在adt-bundle-windows-x86-20130522\sdk\tools目錄下的draw9patch.bat</p>
           

像素密度的計算方式

手機分辨率:800*480(注:手機兩個直角邊上分别放置了800及480個像素點)
手機尺寸大小:3.7英寸(手機斜邊大小)

<span style="font-family:Helvetica, arial, freesans, clean, sans-serif;">//</span>Math.sqrt()是開根号的意思
計算結果:Math.sqrt(800*800+480*480)/3.7 ≈ 252.15dpi
根據google官方文檔(圖1-1)得出,目前手機接近240dpi,則将其歸納在hdpi手機範圍内,即1dp=1.5px。

參照以上方式可将市場上大多數手機劃分為5個像素密度等級,分别為:
ldpi:120dpi,像素密度與dp轉換關系為:1dp = 0.75px
mdpi:160dpi ,像素密度與dp轉換關系為:1dp = 1px
hdpi:240dpi,像素密度與dp轉換關系為:1dp = 1.5px
xhdpi:320dpi,像素密度與dp轉換關系為:1dp = 2px
xxhdpi:480dpi,像素密度與dp轉換關系為:1dp = 3px
           
安卓螢幕适配問題1.權重适配2.圖檔适配3.dimens.xml檔案适配4.布局檔案适配5.java代碼适配 6.其他