本文轉(zhuǎn)自:https://segmentfault.com/a/1190000004492535
譯者:王下邀月熊_Chevalier
前半段翻譯了 后半段準(zhǔn)備自己翻譯 先貼上
Android Support Library 23.2
對于Android Support Library,我們并不能簡單地認(rèn)為它是一個巨石型的庫,而應(yīng)該是一整個致力于提供向后兼容的API的庫的集錦。最新的23.2版本的庫依然添加了很多的新特性,本文中就會進(jìn)行一些說明。
支持 Vector Drawables 以及 Animated Vector Drawables
Vector drawables 允許你將多個png圖片資源替換為單個定義在XML中的向量圖片。該特性在之前被限制在只能用于Lollipop或者更高等級的機(jī)型上,而現(xiàn)在VectorDrawable與AnimatedVectorDrawable都可以在 support-vector-drawable 與 support-animated-vector-drawable中獨(dú)立地使用。
Android Studio 1.4 之前提供了對于vector drawables的有限制性的支持,主要是依賴于在構(gòu)建時將vector drawables轉(zhuǎn)化為png。現(xiàn)在自然是不需要了,為了停用該功能,需要在build.gradle中添加vectorDrawables.useSupportLibrary = true
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
該特性只存在于Gradle Plugin 2.0以上版本, 如果你使用 Gradle 1.5 需要做如下設(shè)置:
// Gradle Plugin 1.5
android {
defaultConfig {
generatedDensities = []
}
// This is handled for you by the 2.0+ Gradle Plugin
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
現(xiàn)在可以在API 7之后使用VectorDrawableCompat,在API 11之后使用AnimatedVectorDrawableCompat。鑒于Android中的drawables的加載機(jī)制,在所有可以使用drawable id的地方都可以支持vector drawables。具體的使用上,當(dāng)使用 ImageView (或者子類 ImageButton 以及 FloatingActionButton), 你可以使用新的 app:srcCompat 屬性來關(guān)聯(lián)到 vector drawables (就像其他使用 android:src的繪圖):
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_add" />;
如果需要在運(yùn)行時動態(tài)改變drawables照樣可以使用 setImageResource()) 方法,不需要任何改變。不過, AppCompat 支持載入vector drawables 當(dāng)它們應(yīng)用在其他的drawable容器中的時候 StateListDrawable, InsetDrawable, LayerDrawable, LevelListDrawable, and RotateDrawable。
AppCompat DayNight theme
本版本也為 AppCompat 添加了一個新的主題: Theme.AppCompat.DayNight.
在API 14之前, DayNight 主題和它的子類DayNight.NoActionBar, DayNight.DarkActionBar, DayNight.Dialog, 與Light等同。 但是在API 14之后就允許在Light以及Dark之間自由切換。默認(rèn)情況下,是否是夜晚取決于系統(tǒng)值 (來自 UiModeManager.getNightMode())), 但是也可以復(fù)寫 AppCompatDelegate.中的關(guān)聯(lián)方法。可以使用靜態(tài)的 AppCompatDelegate.setDefaultNightMode() 方法或者獲取 AppCompatDelegate 通過getDelegate()) 并且使用setLocalNightMode() 來改變目前的 Activity 或者 Dialog 的主題。如果使用的是AppCompatDelegate.MODE_NIGHT_AUTO,會根據(jù)你當(dāng)前的時間以及當(dāng)前所處的位置來自動判斷是否處于夜間,也可以使用MODE_NIGHT_NO或者M(jìn)ODE_NIGHT_YES來手動指定當(dāng)前是否處于夜間模式。
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 standard TextAppearance.AppCompat styles for your text or colors pulled from your theme such as android:textColorPrimary, you’ll find these automatically update for you.
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.
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.
By attaching a BottomSheetBehavior to a child View of a CoordinatorLayout (i.e., addingapp:layout_behavior=”android.support.design.widget.BottomSheetBehavior”), you’ll automatically get the appropriate touch detection to transition between five state:
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 the app:behavior_peekHeight attribute (defaults to 0)
STATE_DRAGGING: the intermediate state while the user is directly dragging the bottom sheet up or down
STATE_SETTLING: that brief time between when the View is released and settling into its final position
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
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
Keep in mind that scrolling containers in your bottom sheet must support nested scrolling (for example,NestedScrollView, RecyclerView, or ListView/ScrollView on API 21+).
If you’d like to receive callbacks of state changes, you can add a 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 aBottomSheetDialog and BottomSheetDialogFragment to fill the modal bottom sheets use case. Simply replaceAppCompatDialog or AppCompatDialogFragment with their bottom sheet equivalents to have your dialog styled as a 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 a number of unique features).
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 supportaudio 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.
RecyclerView
The RecyclerView widget provides an advanced and flexible base for creating lists and grids as well as supportinganimations. This release brings an exciting new feature to the LayoutManager API: auto-measurement! This allows aRecyclerView to size itself based on the size of its contents. This means that previously unavailable scenarios, such as using WRAP_CONTENT for a dimension of the RecyclerView, are now possible. You’ll find all built in LayoutManagers now support auto-measurement.
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.
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.
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.
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.
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 previoussetToolbarColor()) method, you’ll also find a setSecondaryToolbarColor() method for customizing the background color of the bottom bar.
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.
The most visible change may be the introduce of a second column used for action buttons (added by overridingonCreateButtonActions() or calling setButtonActions()). This makes it much easier to reach completion actions without having to scroll through the list of available GuidedActions.
Speaking of GuidedActions, there’s a number of new features to allow richer input including editable descriptions (viadescriptionEditable()), sub actions in the form of a dropdown (with subActions()), and aGuidedDatePickerAction.
These components should make it much easier for you to get information from the user when absolutely required.