资讯专栏INFORMATION COLUMN

Android Support Library 23.2:特性介绍

csRyan / 261人阅读

摘要:最新的版本的库依然添加了很多的新特性,本文中就会进行一些说明。之前提供了对于的有限制性的支持,主要是依赖于在构建时将转化为。鉴于中的的加载机制,在所有可以使用的地方都可以支持。可以使用静态的方法或者获取通过并且使用来改变目前的或者的主题。

时间有限,没能全部翻译,就把图片啥的转到国内的CDN,原文地址这里

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-drawablesupport-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的绘图):

;  

如果需要在运行时动态改变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.

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.

Available Now

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/12613.html

相关文章

  • Material Design - 收藏集 - 掘金

    摘要:与老前辈使用攻略刷新篇掘金小序继使用攻略助力篇之后,一直没有更新上下拉刷新的功能实现,主要还是受限于个人现有的技术实力,总觉得没有经过实际打磨的,就不敢有上场的自信。 DrawerLayout 和 NavigationView 使用详解 - Android - 掘金Android Material Design Library 推出了很长时间,越来越多的APP使用了符合Library ...

    stormzhang 评论0 收藏0
  • Android夜间模式实践

    摘要:现在我采用的就是这种简单粗暴的方法,用户体验比较不友好,后期需要参考知乎的实现,改进实现。参考链接夜间模式最佳实践知乎和简书的夜间模式实现套路夜间模式,你所不知道的坑 前言 由于项目需要,近段时间开发的夜间模式功能。主流的方案如下: 1、通过切换theme实现 2、通过resource id映射实现 3、通过Android Support Library的实现 方案选择 切换the...

    junfeng777 评论0 收藏0
  • Android最佳实践(七)之AppCompat 23.2.0

    摘要:通过附加一个到子视图的添加,通过对的操作我们可以得到它的五种状态。关闭,高度可控属性默认为在用户直接拖动向上或向下的中间状态当被释放和沉降到其最终位置记录的状态展开时的状态默认情况下禁用和启用应用程序属性这使得用户可以向下滑动完全隐藏底部。 这是一个系列,我们将其命名为android最佳实践,如果你还没有看之前的文章: Android最佳实践(一) android最佳实践(二) and...

    mozillazg 评论0 收藏0
  • Android Support Library Revision 24.2.0

    摘要:正式发布更新了版本。的改变当你使用兼容包中的功能时,任何时候的夜间模式的改变都会导致系统自动重新创建。以上便是版本的包的变化,目前来讲,包的分割还是最令人兴奋的,毕竟给瘦身是最能够直接体现在用户身上的。 showImg(https://segmentfault.com/img/bVCU3Y); Google正式发布更新了Android Support Library Revision ...

    zzzmh 评论0 收藏0
  • android 实现【夜晚模式】的另外一种思路

    摘要:上面两个条件说起来很容易,其实想实现并不是很容易的,还好给了我一些思路。同时因为它依赖于,它只能作用于日夜间模式的切换,要想实现换肤功能,是做不到的。 源码地址 在一切开始之前,我只想用正当的方式,跪求各位的一个star showImg(https://segmentfault.com/img/bVyej3); https://github.com/geminiwen/skin-sp...

    stonezhu 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<