天天看點

Android Support Library 23.2

Android Support Library 23.2

Posted by Ian Lake, Developer Advocate 由  Ian Lake 釋出,開發者提倡:

Android Support Library 23.2 安卓支援庫 23.2版本

When talking about the Android Support Library, it is important to realize this isn’t one monolithic library,  but a whole collection of libraries that seek to provide backward-compatible versions of APIs,  as well as offer unique features without requiring the latest platform version. Version 23.2 adds a few new support  libraries as well as new features to many of the existing libraries.

當提及安卓支援庫  (Android Support Library) 的時候,非常重要的一點就是我們必須要認識到它不是一個庫, 而是對APIs尋找一種提供向後 版本相容的 所有支援庫的集合 。以及提供一個統一的樣式且并不需要最新平台版本。23.2版本增加了一些新的支援庫以及 一些現有庫的新特性。

Support Vector Drawables and Animated Vector Drawables 支援矢量圖檔和帶動畫的矢量圖檔

Vector drawables allow you to replace multiple png assets with a single vector graphic, defined in XML.  While previously limited to Lollipop and higher devices, both VectorDrawable andAnimatedVectorDrawable are now  available through two new Support Libraries support-vector-drawable and animated-vector-drawable, respectively. 矢量圖檔(Vector drawables) 允許你僅使用一個定義在XML中的矢量的圖形來替換多個png圖檔資源。而以前這種特性限制在棒棒糖 (  Lollipop )以及以上的裝置上才有效,但是現在可以分别通過兩個新的支援庫 support-vector-drawable 和animated-vector-drawable 來實作。

Android Studio 1.4 introduced limited support for vector drawables by generating pngs at build time. To disable  this functionality (and gain the true advantage and space savings of this Support Library), you need to add  vectorDrawables.useSupportLibrary = true to your build.gradle file:

Android Studio 1.4 介紹了一種通過在編譯時生成png圖檔的辦法對矢量圖檔提供了有限的支援。為了禁用這個功能(獲得支援庫真正的優勢和節省空間) ,你需要添加 vectorDrawables.useSupportLibrary = true 到你的 build.gradle 檔案裡面。

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

You’ll note this new attribute only exists in the version 2.0 of the Gradle Plugin. If you are using Gradle 1.5 you’ll  instead use 你會發現這個新的屬性隻存在于Gradle  Plugin  2.0版本(或更高版本)。如果你使用 Gradle  Plugin  1.5,你可以使用以下配置來代替

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

  // This is handled for you by the 2.0+ Gradle Plugin  
  aaptOptions {  
    additionalParameters "--no-version-vectors"  
  }  
 }  
           

You’ll be able to use VectorDrawableCompat back to API 7 and AnimatedVectorDrawableCompat on all API 11 and higher devices.  Due to how drawables are loaded by Android, not every place that accepts a drawable id (such as in an XML file) will support  loading vector drawables. Thankfully, AppCompat has added a number of features to make it easy to use your new vector  drawables.

你現在可以在API7上使用 VectorDrawableCompat,在API11上使用  AnimatedVectorDrawableCompat 。介于安卓加載圖檔的方法,并不是所有接受 一個圖檔ID(比如在XML中的)的地方都支援加載矢量圖檔。值得慶幸的是,AppCompat已經添加了大量的特性來讓你在使用新的矢量圖檔時變得很 簡單。

Firstly, when using AppCompat with ImageView (or subclasses such as ImageButton and FloatingActionButton), you’ll  be able to use the new app:srcCompat attribute to reference vector drawables (as well as any other drawable  available to android:src):

首先,當你在 ImageView (或者他的子類比如 ImageButton 和 FloatingActionButton )上使用 AppCompat時,你可以使用新的屬性 app:srcCompat 來引用矢量圖檔(也可以是其他任何可使用 android:src 的可繪制的控件):

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

And if you’re changing drawables at runtime, you’ll be able to use the same setImageResource() method as before - no changes there. Using AppCompat and app:srcCompat is the most foolproof method of integrating vector drawables into your app.

并且,如果你想在運作時改變圖檔,你可以像以前一樣,使用 setImageResource()  方法 - 在這裡并沒有改變什麼。 在你的應用裡面 使用  AppCompat 和  app:srcCompat 來整合矢量圖檔是最有效的。

You’ll find directly referencing vector drawables outside of app:srcCompat will fail prior to Lollipop. However, AppCompat does support loading vector drawables when they are referenced in another drawable container such as a StateListDrawable, InsetDrawable, LayerDrawable, LevelListDrawable, and RotateDrawable. By using this indirection, you can use vector drawables in cases such as TextView’s android:drawableLeft attribute, which wouldn’t normally be able to support vector drawables.

Lollipop之前版本, 你會發現 在  app:srcCompat 外 直接引用矢量圖檔會失敗。但是, AppCompat 卻支援其他的drawable 容器加載矢量圖檔,比如StateListDrawable, InsetDrawable, LayerDrawable, LevelListDrawable和 RotateDrawable等。通過這種間接的方法,你能在諸如TextView 的amdroid:drawableLeft 屬性  等通常是不支援矢量圖檔的容器裡面使用矢量圖檔了。

AppCompat DayNight theme

While enabling the use of vector graphics throughout your app is already a large change to AppCompat, there’s a new theme added to AppCompat in this release: Theme.AppCompat.DayNight.

對于 AppCompat來說,在你整個的應用裡面使用矢量圖形成為了可能已經是一個非常大的改變了,但是,這次釋出還加入了一個新的主題:

Theme.AppCompat.DayNight.

Android Support Library 23.2
Android Support Library 23.2

Prior to API 14, The DayNight theme and its descendents DayNight.NoActionBar, DayNight.DarkActionBar, DayNight.Dialog, etc. become their Light equivalents. But on API 14 and higher devices, this theme allows apps to easily support both a Light and Dark theme, effectively switching from a Light theme to a Dark theme based on whether it is ‘night’.

在API 14之前, DayNight主題以及它的後代 DayNight.NoActionBar, DayNight.DarkActionBar, DayNight.Dialog等等和Light是一樣的。但是在API 14以及以上的裝置上,這個主題讓應用更簡單的支援白天和黑夜兩個主題。

By default, whether it is ‘night’ will match the system value (from UiModeManager.getNightMode()), but you can override that value with methods in AppCompatDelegate. You’ll be able to set the default across your entire app (until process restart) with the static AppCompatDelegate.setDefaultNightMode() method or retrieve an AppCompatDelegate via getDelegate() and use setLocalNightMode() to change only the current Activity or Dialog.

預設的情況下,是否是“晚上”将由系統值來決定(從UiModeManager.getNightMode()),但是你可以通過 AppCompatDelegate 裡的方法來覆寫這個值。你可以在你的整個應用裡面(直到程序重新開機)使用靜态方法 AppCompatDelegate.setDefaultNightMode() 來設定這個預設值,或者通過 getDelegate() 來擷取一個  AppCompatDelegate然後使用  setLocalNightMode() 來改變僅僅目前activity或者Dialog的 預設值。

When using AppCompatDelegate.MODE_NIGHT_AUTO, the time of day and your last known location (if your app has the location permissions) are used to automatically switch between day and night, while MODE_NIGHT_NO and MODE_NIGHT_YES forces the theme to never or always use a dark theme, respectively.

當使用  AppCompatDelegate.MODE_NIGHT_AUTO,根據當天的時間以及你最近一次已知的位置資訊(如果你的應用有擷取位置資訊的權限的話)将會被用來自動卻換白天和黑夜主題。 MODE_NIGHT_NO 和MODE_NIGHT_YES分别表示強制不适用和總是使用黑色主題。

It is critical that you test your app thoroughly when using the DayNight themes as hardcoded colors can easily make for unreadable text or icons. If you are using the standardTextAppearance.AppCompat styles for your text or colors pulled from your theme such as android:textColorPrimary, you’ll find these automatically update for you.

至關重要的是,務必要徹底測試一下你的應用,因為在  DayNight 主題下使用寫死書寫顔色會很容易造成文本或圖檔的 不可讀 。如果你文本使用的是标準的 TextAppearance.AppCompat 樣式或者顔色是 從主題裡面(比如android:textColorPrimary )  得到的,你将發現這些将為你自動更新視圖。

However, if you’d like to customize any resources specifically for night mode, AppCompat reuses the night resource qualifier folder, making it possible customize every resource you may need. Please consider using the standard colors or taking advantage of the tinting support in AppCompat to make supporting this mode much easier.

然而, 如果你想專門為夜間模式自定義自己的一套資源,正由于 AppCompat 是重用夜間模式資源限定符的,這讓自定義你所需要的所有資源成為可能。  為了讓對這個模式的支援變得更輕松,請考慮使用标準的顔色或者使用AppCompat中有優勢的着色。

Design Support Library: Bottom Sheets

The Design Support Library provides implementations of many patterns of material design. This release allows developers to easily add bottom sheets to their app.

Design Support Library 提供了許多 materia design 設計模式的實作。 此次釋出讓開發者更加容易的在他們的應用上添加 bottom sheets。

Android Support Library 23.2

By attaching a BottomSheetBehavior to a child View of a CoordinatorLayout (i.e., adding app:layout_behavior="android.support.design.widget.BottomSheetBehavior"), you’ll automatically get the appropriate touch detection to transition between five state:

通過在 CoordinatorLayout (比如添加 app:layout_behavior="android.support.design.widget.BottomSheetBehavior"  )的子View裡面加上一個  BottomSheetBehavior ,你可以  你将自動得到能在5個狀态間切換的觸摸檢測功能:

  • STATE_COLLAPSED: this collapsed state is the default and shows just a portion of the layout along the bottom. The height can be controlled with theapp:behavior_peekHeight attribute (defaults to 0)
  • STATE_COLLAPSED:預設狀态:折疊的狀态,隻顯示布局底部的一部分。它的高度可以通過      app:behavior_peekHeight 屬性來控制(預設高度為0)。
  • STATE_DRAGGING: the intermediate state while the user is directly dragging the bottom sheet up or down
  • STATE_DRAGGING:中間狀态:此時使用者可以直接上下拖動bottom sheet
  • STATE_SETTLING: that brief time between when the View is released and settling into its final position
  • STATE_SETTLING:在View被釋放到View滑到最底部之間有一個短暫的時間
  • STATE_EXPANDED: the fully expanded state of the bottom sheet, where either the whole bottom sheet is visible (if its height is less than the containing CoordinatorLayout) or the entire CoordinatorLayout is filled
  • 不管是整個 bottom sheet 都是可見的(如果它的高度比所包含的CoordinatorLayout 還要小,即一屏就能顯示) 還是整個 CoordinatorLayout 都被填充滿了(一屏顯示不夠)都會将 bottom sheet  全部展開
  • STATE_HIDDEN: disabled by default (and enabled with the app:behavior_hideable attribute), enabling this allows users to swipe down on the bottom sheet to completely hide the bottom sheet
  • 預設這種狀态是被禁止的(可以通過使用 app:behavior_hideable 屬性來開啟),如果開啟了這種狀态,允許使用者向下滑動bottom sheet 直到完全隐藏掉 bottom sheet

Keep in mind that scrolling containers in your bottom sheet must support  nested scrolling  (for example, NestedScrollView, RecyclerView, or ListView/ScrollView on API 21+).

記住。 bottom sheet 滾動容器必須支援 nested scrolling (比如 NestedScrollView,RecyclerView,或者API 21以上的ListView和ScrollView)

If you’d like to receive callbacks of state changes, you can add a BottomSheetCallback:

如果你想接收一個狀态改變時的回調,你可以添加一個  BottomSheetCallback:

// The View with the BottomSheetBehavior  
 View bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet);  
 BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);  
 behavior.setBottomSheetCallback(new BottomSheetCallback() {  
    @Override  
    public void onStateChanged(@NonNull View bottomSheet, int newState) {  
      // React to state change  
    }  
      @Override  
      public void onSlide(@NonNull View bottomSheet, float slideOffset) {  
       // React to dragging events  
   }  
 });  
           

While BottomSheetBehavior captures the persistent bottom sheet case, this release also provides a BottomSheetDialog and BottomSheetDialogFragment to fill the modal bottom sheets use case. Simply replace AppCompatDialog or AppCompatDialogFragment with their bottom sheet equivalents to have your dialog styled as a bottom sheet.

盡管  BottomSheetBehavior 針對的是 persistent bottom sheet 情景,但是這次釋出也提供了一個  BottomSheetDialog 和 BottomSheetDialogFragment 來填補 modal bottom sheets 用例。可以簡單地使用bottm sheet的替代品更換 AppCompatDialog 或者 AppCompatDialogFragment使你的對話框呈現出 bottom sheet的風格。

Support v4: MediaBrowserServiceCompat

The Support v4 library serves as the foundation for much of the support libraries and includes backports of many framework features introduced in newer versions of the platform (as well anumber of unique features).

Support v4 library 作為很多支援庫的基礎,包含了很多在新的平台上才被才被引入的 framewor特性(以及幾個 獨特的功能)。

Adding onto the previously released MediaSessionCompat class to provide a solid foundation for media playback, this release adds MediaBrowserServiceCompat and MediaBrowserCompat providing a compatible solution that brings the latest APIs (even those added in Marshmallow) back to all API 4 and higher devices. This makes it much easier to support audio playback on Android Auto and browsing through media on Android Wear along with providing a standard interface you can use to connect your media playback service and your UI.

上個版本釋出的 MediaSessionCompat 類裡面已經為媒體播放提供一個結實的基礎,這次釋出新添加的  MediaBrowserServiceCompat 和  MediaBrowserCompat 提供了一個相容性上的解決辦法,做到了最新的APIs(甚至這些已經被添加到  Marshmallow )向後相容到API 4及以上裝置。這讓支援 安卓車載 上的音頻播放,安卓手表上的媒體浏覽變得更加簡單,另外還為連接配接媒體播放服務和 UI提供了一個标準的接口。 

RecyclerView

The RecyclerView widget provides an advanced and flexible base for creating lists and grids as well as supporting animations. This release brings an exciting new feature to theLayoutManager API: auto-measurement! This allows a RecyclerView to size itself based on the size of its contents. This means that previously unavailable scenarios, such as usingWRAP_CONTENT for a dimension of the RecyclerView, are now possible. You’ll find all built in LayoutManagers now support auto-measurement.

RecyclerView 為建立清單和表格提供了一個先進而且很靈活的基礎,并且還支援動畫 animations。這次釋出為 LayoutManager API  帶來了一個令人興奮的新特性:自動測量!這讓  RecyclerView 可以基于它的内容來測量自身的大小。這就意味着一些以前不可能實作的情況現在成為了能,比如使用為  RecyclerView 的尺寸使用  WRAP_CONTENT。你會發現所有内置  LayoutManagers 現在都支援自動測量了。

Due to this change, make sure to double check the layout parameters of your item views: previously ignored layout parameters (such as MATCH_PARENT in the scroll direction) will now be fully respected.

由于這個改變,一定要仔細檢查 item views 的布局參數:以前一直被忽略的布局參數(比如 在滾動方向上的  MATCH_PARENT )現在将會被認真地對待。

If you have a custom LayoutManager that does not extend one of the built in LayoutManagers, this is an opt-in API - you’ll be required to call setAutoMeasureEnabled(true) as well as make some minor changes as detailed in the Javadoc of the method.

如果你有一個自定義的  LayoutManager   而不是去擴充一個内置的  LayoutManagers,這是一個API的選擇,你需要調用 setAutoMeasureEnabled(true) ,關于這個方法的一些小變化請參考  Javadoc(java文檔)。

Note that although RecyclerView animates its children, it does not animate its own bounds changes. If you would like to animate the RecyclerView bounds as they change, you can use the Transition APIs.

注意,雖然  RecyclerView 可以給給子 view 添加動畫,但是他的邊界的改變是沒有動畫的,如果你想在  RecyclerView 邊界上也加上動畫,你可以使用過渡動畫。

Custom Tabs

Custom Tabs makes it possible to seamlessly transition to web content while keeping the look and feel of your app. With this release, you’ll now be able to add actions to a bottom bar for display alongside the web content.

自定義标簽可以在保持你應用的外觀與風格的同時無縫的過渡到網頁内容。通過這個版本,你現在可以在網頁内容旁邊添加一個底部按鈕操作項。

Android Support Library 23.2

With the new addToolbarItem() method, you’ll be able to add up to currently 5 (MAX_TOOLBAR_ITEMS) actions to the bottom bar and update them with setToolbarItem() once the session has begun. Similar to the previous setToolbarColor() method, you’ll also find a setSecondaryToolbarColor() method for customizing the background color of the bottom bar.

使用這個新的  addToolbarItem() 方法,你目前可以添加至多5個( MAX_TOOLBAR_ITEMS )操作到底部按鈕,一旦回話開始,再用  setToolbarItem()  方法更新他們,你還會發現一個  setSecondaryToolbarColor() 方法,用來自定義底部按鈕的背景顔色。

Leanback for Android TV

The Leanback Library gives you the tools you need to easily bring your app to Android TV with many standard components optimized for the TV experience. The GuidedStepFragment received a significant set of improvements with this release.

Leanback Library 給了你需要的工具,友善你把你的應用帶到 Android TV 上,裡面還有需要專門為TV而優化的元件。 這次釋出,  GuidedStepFragment 得到了一個顯著的改善。

Android Support Library 23.2

The most visible change may be the introduce of a second column used for action buttons (added by overriding onCreateButtonActions() or calling setButtonActions()). This makes it much easier to reach completion actions without having to scroll through the list of available GuidedActions.

最直覺的改變可能就是在動作按鈕上(可以通過複寫 onCreateButtonActions() 或者 調用 setButtonActions()來添加 )引入第二列。這将使得我們不必再滾動可見的 GuidedActions 清單就可以完成所有動作。

Speaking of GuidedActions, there’s a number of new features to allow richer input including editable descriptions (via descriptionEditable()), sub actions in the form of a dropdown (with subActions()), and a GuidedDatePickerAction.

提到  GuidedActions,有一系列的讓輸入更加豐富的新特性,包括輸入框的描述(通過  descriptionEditable() ),下拉輸入框的二級操作和一個  GuidedDatePickerAction。

Android Support Library 23.2

These components should make it much easier for you to get information from the user when absolutely required.

在你最需要的時候, 這些元件一定會 讓你 更加容易的 從使用者那裡得到資訊。

Available Now

Version 23.2 of the Android Support Library is available via your SDK Manager and Android Studio. Take advantage of all of  the new features as well as additional bug fixes starting now! As always, file bug reports at b.android.com and connect  with other developers on the Android Development Google+ community.

Android Support Library 23.2 現在已經可以通過 SDK Manager 和 Android Studio 下載下傳使用了。充分利用這些新特性吧,以及一些  附加的bug修複已經開始了,檔案錯誤報告在  b.android.com,和一些其他開發者一起交流  Android  開發 Google+ 社群 Posted by Reto Meier at 2:19 PM Translator : hymanme at 2016-3-3

繼續閱讀