[轉(zhuǎn)載]Android Support Library 23.2:特性介紹

本文轉(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.

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.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,505評論 6 533
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,556評論 3 418
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,463評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,009評論 1 312
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,778評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,218評論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,281評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,436評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,969評論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,795評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,993評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,537評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,229評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,659評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,917評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,687評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,990評論 2 374

推薦閱讀更多精彩內(nèi)容