Android一點 打造全功能屏幕適配AutoView

android適配是繁瑣的事,分辨率多樣,考慮因素多,並且ui設計圖還是px單位的,這時候你就需要AutoView了,AutoView可以幫你做什麼呢?寫多個dimens文件? 還是需要寫很多的代碼?no,這些都不用了,開發還是和之前的一樣,而且還可以輕鬆的和ui設計圖對應上

AutoView使用大法

傳送門AutoView:https://github.com/Flyjun-Android/AutoView

1、你的build.gradle需要

compile project(':AutoViewCore')

2、在你的BaseActivity上調用

AutoView.init(this);

 or

 AutoView.init(this,1080.0f);

第二個參數爲你ui設計圖設計的基準尺寸(比如1080 * 1920就是1080,720 * 1280就是720,默認是1080的) 需要在setContentView之前執行

3、在你的佈局layout文件中這麼使用

<?xml version="1.0" encoding="utf-8"?>
<com.flyjun.view.AutoLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="vertical"
android:id="@+id/layout"
>


<TextView
    android:layout_width="200px"
    android:layout_height="200px"
    android:textSize="50px"
    android:textColor="@android:color/black"
    android:background="@android:color/darker_gray"
    android:text="hello"/>


<com.flyjun.view.AutoRelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="520px"
        android:layout_height="520px"
        android:textSize="80px"
        android:textColor="@android:color/holo_red_light"
        android:background="@android:color/holo_green_light"
        android:text="AutoView"/>

</com.flyjun.view.AutoRelativeLayout>


<include layout="@layout/inlayout"/>

</com.flyjun.view.AutoLinearLayout>

你只需要做的的是

   LinearLayout->AutoLinearLayout 

   RelativeLayout->AutoRelativeLayout

   FrameLayout->AutoFrameLayout

注意,如果你的跟佈局也需要適配,需要加上

xmlns:auto="http://schemas.android.com/apk/res-auto"
auto:autoParents="true"

4、AutoView支持的屬性幾乎涵蓋了所有

android.R.attr.textSize

        android.R.attr.padding
        android.R.attr.paddingLeft
        android.R.attr.paddingTop
        android.R.attr.paddingRight
        android.R.attr.paddingBottom

        android.R.attr.layout_width
        android.R.attr.layout_height

        android.R.attr.layout_margin
        android.R.attr.layout_marginLeft
        android.R.attr.layout_marginTop
        android.R.attr.layout_marginRight
        android.R.attr.layout_marginBottom

        android.R.attr.drawablePadding

5、使用代碼也可以輕鬆的適配view,builder模式調用 例如:

AutoView.autoBuilder(view).setWidth(320).setHeight(120).setMarginTop(50).builder();

6、你還可以獲取一個已經適配好的值

AutoView.getAutoSize(this,120)

7、如果需要適配橫豎屏,那麼需要values-land的dimens和values的dimens一起來操作即可 比如都有一個寬度width,都引用這個值即可

8、look look 效果圖

720*1280效果圖
這裏寫圖片描述

1080*1920效果圖
這裏寫圖片描述

1536*2048 nexus9平板效果圖
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章