Android自動布局,一次編寫全部設(shè)備適用

在做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è)計圖:


Paste_Image.png

這樣的設(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è)計圖,自動適配。

用法

  1. 設(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)
  1. 對與長寬一致性問題
    由于長寬縮放的存在,所有在布局文件中,長寬同時設(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"表示高度的尺寸縮放參考寬度。

  1. 適配模式
    這個庫默認對所有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" />
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容