在做android項目中,遇到最多的問題就是適配,各種尺寸的機型,讓開發(fā)者頭痛,自己在開發(fā)中也存在這個問題,不過我目前的公司大多項目是demo的性質(zhì),但是最近一個產(chǎn)品化的項目,讓我在適配問題上遇到了不少困難。
于是想要使用別人的研究成果,就找到了鴻祥大神寫的https://github.com/hongyangAndroid/AndroidAutoLayout 庫,他的設(shè)計思路是根據(jù)UI設(shè)計的效果圖尺寸,然后在不同手機上根據(jù)比例進行縮放,感覺滿足絕大多數(shù)適配需求,可惜大神太忙,這個庫已經(jīng)停止維護了。
針對鴻祥的設(shè)計思路,自己也寫了一個自動布局的庫https://github.com/tenny1225/AndroidAutoLayout,目地也是使用方式簡單,盡量少改現(xiàn)有的代碼。以下有部分照搬鴻祥博客里面的內(nèi)容
假設(shè)我們拿到一張設(shè)計圖:

這樣的設(shè)計圖開發(fā)中很常見吧,有些公司可能需要自己去測量。
按照我們的思想:
布局直接抄設(shè)計圖上的尺寸
對于布局文庫應(yīng)該這么寫:
<com.xz.autoLayout.AutoRelativeLayout
android:layout_width="match_parent"
android:layout_height="86px"
android:layout_marginTop="26px"
android:background="#ffffffff">
<ImageView
android:id="@+id/id_tv_add"
android:layout_width="34px"
android:layout_height="34px"
android:layout_gravity="center_vertical"
android:layout_marginLeft="276px"
android:layout_marginTop="26px"
android:src="@mipmap/add"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="26px"
android:layout_toRightOf="@id/id_tv_add"
android:text="新增旅客"
android:textColor="#1fb6c4"
android:textSize="32px"
/>
</com.xz.autoLayout.AutoRelativeLayout>
可以看到只有第一個RelativeLayout修改成了自定義的AutoRelativeLayout,就可以做到根據(jù)設(shè)計圖,自動適配。
用法
- 設(shè)定設(shè)計圖尺寸
在Application的oncreate方法中加入:
AutoLayoutManager.init(this,720,1280,false);//720×1280是ui設(shè)計圖的尺寸
//第四隔參數(shù)是設(shè)計圖是否有狀態(tài)欄
2.修改布局文件
將所有布局文件中的第一個viewgroup修改成相應(yīng)的Auto...Layout,目前實現(xiàn)的有
AutoLinearLayout,AutoRelativeyout,AutoFrameLayout,對于在其他的viewgroup,可以實現(xiàn)IAuto接口重寫相應(yīng)方法進行適配,具體參考AutoLinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<com.xz.autoLayout.AutoLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="140px"
android:layout_height="140px"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="80px"
android:layout_height="80px"
android:src="@mipmap/ic_launcher"
android:tag="w-h" />
<TextView
android:id="@+id/tv_action_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8px"
android:gravity="center"
android:text="@string/next"
android:textColor="?attr/actionBarTextColor"
android:textSize="22px"
/>
</com.xz.autoLayout.AutoLinearLayout>
4.直接適配
可以調(diào)用auto方法進行適配,例如在baseActivity或者baseFragment中使用
AutoUtil.auto(View v)
- 對與長寬一致性問題
由于長寬縮放的存在,所有在布局文件中,長寬同時設(shè)置為200px,其實最后顯示他們長寬是不同的,此時可以設(shè)置tag "w-h"和"h-w"屬性
<TextView
android:layout_width="85px"
android:layout_height="70px"
android:layout_alignTop="@+id/iv_edit"
android:layout_marginLeft="30px"
android:tag="w-h"
android:layout_marginTop="30px"
android:background="#aaffffff"
android:gravity="center"
android:text="@string/compare"
android:textColor="?attr/actionBarTextColor" />
"w-h"表示寬度的尺寸縮放參考高度,"h-w"表示高度的尺寸縮放參考寬度。
- 適配模式
這個庫默認對所有view的所有屬性進行了適配,所以就算你設(shè)置了某個屬性值為12dp,它還是把他當做px對待進行縮放,所以對于不需要進行縮放的view,可以在tag中加入相應(yīng)標志,讓庫忽略掉對應(yīng)屬性,下表面列出來可識別的tag標志
not表示對于view的所有屬性都不適配
!w不適配寬度
!h不適配高度
!p不適配padding
!pl不適配paddingLeft
!pt不適配paddingTop
!pr不適配paddingRight
!pb不適配paddingBottom
!m不適配margin
!ml不適配marginLeft
!mt不適配marginTop
!mr不適配marginRight
!mb不適配marginBottom
!maw不適配maxWidth
!mah不適配maxHeight
!miw不適配minWidth
!mih不適配minHeight
!ts不適配TextSize
w-hwidth參照height縮放
h-wheight參照width縮放
ml-hmarginLeft參照height縮放
mr-hmarginRight參照height縮放
mt-wmarginTop參照width縮放
mb-wmarginBottom參照width縮放
pl-hpaddingLeft參照height縮放
pr-hpaddingRight參照height縮放
pt-wpaddingTop參照width縮放
pb-wpaddingBottom參照width縮放
maw-hmaxWidth參照height縮放
mah-wmaxHeight參照width縮放
miw-hminWidth參照height縮放
mih-wminHeight參照width縮放
ts-wTextSize參照width縮放
<TextView
android:layout_width="85px"
android:layout_height="70px"
android:layout_alignTop="@+id/iv_edit"
android:layout_marginLeft="30px"
android:tag="w-h,ml-h" <!--width參照height縮放,marginLeft參照height縮放-->
android:layout_marginTop="30px"
android:background="#aaffffff"
android:gravity="center"
android:text="@string/compare"
android:textColor="?attr/actionBarTextColor" />