Android Support Library 23.2

qrgs8398 10年前發布 | 17K 次閱讀 安卓開發 Android開發 移動開發

來自: http://blog.chengyunfeng.com/?p=869

本周 Android Support 庫發布了 23.2新版本,該版本包含了幾個新功能。

支持 Vector Drawables 和 Animated Vector Drawables

使用矢量圖的好處之一就是不用出多個圖片(hdpi, xhdpi 等)了, 只需要一個矢量圖即可涵蓋所有屏幕密度的需要。矢量圖在 Android 5.0 才開始支持,使用 23.2 support 庫中的 support-vector-drawable 和 animated-vector-drawable 可以在之前的版本支持矢量圖了。之前Android studio 在 build 的時候, 通過 生成 PNG 圖片來支持矢量圖 ,要使用 23.2 版本中的矢量圖功能,需要禁用之前版本的功能。如果你使用的 gradle 插件為 2.0+ 版本,則通過如下方式來啟用新功能:

Java

// Gradle Plugin 2.0+  
 android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
    }  
 }
 // Gradle Plugin 2.0+  
 android {  
  defaultConfig {  
    vectorDrawables.useSupportLibrary = true  
    }  
 }  

如果你使用 1.5 的 gradle 插件,則需要通過如下的方式:

Java

// Gradle Plugin 1.5  
 android {  
   defaultConfig {  
     generatedDensities = []  
  }  

  // This is handled for you by the 2.0+ Gradle Plugin  
  aaptOptions {  
    additionalParameters "--no-version-vectors"  
  }  
 }
// Gradle Plugin 1.5  
 android {  
  defaultConfig {  
    generatedDensities = []  
  }  
 
  // This is handled for you by the 2.0+ Gradle Plugin  
  aaptOptions {  
    additionalParameters "--no-version-vectors"  
  }  
 }  

然后就可以使用 VectorDrawableCompat 和 AnimatedVectorDrawableCompat 了,VectorDrawableCompat 最低可以在 API 7 版本上使用, 而 AnimatedVectorDrawableCompat 需要 API 11 版本。目前通過 AppCompat 庫只支持 ImageView 和其子類(例如 ImageButton 和 FloatingActionButton)可以在布局文件中直接使用 矢量圖,支持方式為使用 app:srcCompat 來替代 android:src,

XHTML

<ImageView  
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  app:srcCompat="@drawable/ic_add" />
<ImageView  
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  app:srcCompat="@drawable/ic_add"/>  

如果在代碼中設置矢量圖,則和之前一樣,可以直接使用 setImageResource() 。使用 AppCompat 和 app:srcCompat 是使用矢量圖最簡單的方式。

在5.0之前的版本除了 app:srcCompat 屬性之外,其他屬性都不支持矢量圖,例如 TextView 的 android:drawableLeft ,你如果在該屬性上使用矢量圖,則在5.0之前的版本上會crash。但是,你如果把矢量圖放到其他 Drawable 容器中,再使用這個 Drawable 容器可以支持5.0之前的版本,支持的容器有 StateListDrawable, InsetDrawable, LayerDrawable, LevelListDrawable, 和 RotateDrawable。

你可以在 Drawable 容器中應用你的矢量圖,例如通過 StateListDrawable 來引用矢量圖,然后在 TextView 的 android:drawableLeft 中使用這個 StateListDrawable,這樣在 5.0 之前的版本也可以使用矢量圖了。

這樣每個矢量圖都需要放到一個 Drawable 容器中,可能比較繁瑣,如果你使用 Data Binding 框架,則還可以又一個相對方便的方式。

定義一個 BindingAdapter, 該 BindingAdapter 把一個Drawable id 綁定到 TextView 的 drawableTop,

Java

public class Bindings {

    @BindingAdapter({"bind:topId"})
    public static void loadImage(TextView view, int topResId) {
        view.setCompoundDrawablesWithIntrinsicBounds(0,topResId, 0,0);

    }
}
public class Bindings {
 
    @BindingAdapter({"bind:topId"})
    public static void loadImage(TextViewview, int topResId) {
        view.setCompoundDrawablesWithIntrinsicBounds(0,topResId, 0,0);
 
    }
}

然后在 布局文件中,使用這個自定義的 BindingAdapter,

XHTML

<?xml version="1.0" encoding="utf-8"?>
<layout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:bind="http://schemas.android.com/apk/res-auto">
    <data>
        <import type="org.goodev.myapplication.R" />
    </data>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            bind:topId="@{R.drawable.ic_favorite_outline_24dp}"
            android:text="Hello World!" />
</layout>
<?xmlversion="1.0" encoding="utf-8"?>
<layout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:bind="http://schemas.android.com/apk/res-auto">
    <data>
        <importtype="org.goodev.myapplication.R"/>
    </data>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            bind:topId="@{R.drawable.ic_favorite_outline_24dp}"
            android:text="Hello World!"/>
</layout>

AppCompat DayNight 主題

除了支持矢量圖以外,23.2版本中還保護了一個新的 Theme.AppCompat.DayNight主題。

在 API 14之前的版本,DayNight 主題和其子主題 ( DayNight.NoActionBar, DayNight.DarkActionBar, DayNight.Dialog 等) 都使用 白色主題替代。在 14+版本,則 DayNight 可以使應用很容易的同時支持白色和黑色主題,并且可以根據用戶當前的時間自動切換主題,白天使用白色主題,夜晚使用黑色主題,是不是很酷。

默認情況下系統使用 UiModeManager.getNightMode() 來判斷是否使用黑色主題。在 AppCompatDelegate 中可以重寫該方法。通過函數 AppCompatDelegate.setDefaultNightMode() 可以設置整個應用的模式,也可以通過 getDelegate().setLocalNightMode() 函數來修改當前 Activity 和 Dialog 的主題。

如果使用 AppCompatDelegate.MODE_NIGHT_AUTO 模式,系統會根據當前的時間和設備最后已知的位置(需要您的應用具有位置權限)來自動切換主題。

如果你使用了 DayNight ,請全面測試你的應用在每個主題都可以使用,特別是文本和圖標的顏色,如果你使用標準的 TextAppearance.AppCompat 來設置文字樣式,或者從 android:textColorPrimary 來獲取顏色,則系統會自動切換到合適的顏色。

除了這兩個比較大的新功能以外,23.2 版本中還有如下改動,詳細情況請 參考這里

  • Design 庫中新加一個 bottom sheet 控件。

  • Support v4 的 MediaBrowserServiceCompat

  • RecyclerView 的自動計算本身大小的功能,這樣 RecyclerView 就可以根據其內容的大小來設置自己的尺寸了。

  • Custom Tabs 更加好用。

  • Leanback for Android TV 中新的 GuidedStepFragment ,更加方便的創建新手指引。

 本文由用戶 qrgs8398 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!