[转载]Android Support Library 23.2:特性介绍

本文转自:https://segmentfault.com/a/1190000004492535
译者:王下邀月熊_Chevalier
前半段翻译了 后半段准备自己翻译 先贴上

Android Support Library 23.2
对于Android Support Library,我们并不能简单地认为它是一个巨石型的库,而应该是一整个致力于提供向后兼容的API的库的集锦。最新的23.2版本的库依然添加了很多的新特性,本文中就会进行一些说明。

支持 Vector Drawables 以及 Animated Vector Drawables

Vector drawables 允许你将多个png图片资源替换为单个定义在XML中的向量图片。该特性在之前被限制在只能用于Lollipop或者更高等级的机型上,而现在VectorDrawable与AnimatedVectorDrawable都可以在 support-vector-drawable 与 support-animated-vector-drawable中独立地使用。

Android Studio 1.4 之前提供了对于vector drawables的有限制性的支持,主要是依赖于在构建时将vector drawables转化为png。现在自然是不需要了,为了停用该功能,需要在build.gradle中添加vectorDrawables.useSupportLibrary = true

 // Gradle Plugin 2.0+  

 android {  

   defaultConfig {  

     vectorDrawables.useSupportLibrary = true  

    }  
 }  

该特性只存在于Gradle Plugin 2.0以上版本, 如果你使用 Gradle 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"  

      }  

 }  

现在可以在API 7之后使用VectorDrawableCompat,在API 11之后使用AnimatedVectorDrawableCompat。鉴于Android中的drawables的加载机制,在所有可以使用drawable id的地方都可以支持vector drawables。具体的使用上,当使用 ImageView (或者子类 ImageButton 以及 FloatingActionButton), 你可以使用新的 app:srcCompat 属性来关联到 vector drawables (就像其他使用 android:src的绘图):

<ImageView  

  android:layout_width="wrap_content"  

  android:layout_height="wrap_content"  

  app:srcCompat="@drawable/ic_add" />;  

如果需要在运行时动态改变drawables照样可以使用 setImageResource()) 方法,不需要任何改变。不过, AppCompat 支持载入vector drawables 当它们应用在其他的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之间自由切换。默认情况下,是否是夜晚取决于系统值 (来自 UiModeManager.getNightMode())), 但是也可以复写 AppCompatDelegate.中的关联方法。可以使用静态的 AppCompatDelegate.setDefaultNightMode() 方法或者获取 AppCompatDelegate 通过getDelegate()) 并且使用setLocalNightMode() 来改变目前的 Activity 或者 Dialog 的主题。如果使用的是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 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.

586354117-5701d1c590f70_articlex.png

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.

2924115873-5701d1c6095ae_articlex.png

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.

928249444-5701d1ce50956_articlex.png

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容