Android最佳實踐(七)之AppCompat 23.2.0

ufosaga 8年前發布 | 52K 次閱讀 安卓開發 Android開發 移動開發

來自: https://segmentfault.com/a/1190000004519874

這是一個系列,我們將其命名為android最佳實踐,如果你還沒有看之前的文章:

Android最佳實踐(一)

android最佳實踐(二)

android最佳實踐(三)

android最佳實踐(四)

android最佳實踐(五)

Android最佳實踐(六)之掃描二維碼模塊

在2016年的2月24日,google的Android開發團隊發布了:

compile 'com.android.support:appcompat-v7:23.2.0'

那么其和我們現階段用的23.1有什么區別呢,在這篇博客中,將會講解appcompat23.2的新特性以及編寫相關實驗代碼,供大家一起學習。

那么,就從我最喜歡的AppCompat DayNight theme和 Bottom Sheets講起吧

AppCompat DayNight theme

其提供了日夜模式,其對應有四種狀態:

public static final int MODE_NIGHT_NO = 1;

public static final int MODE_NIGHT_YES = 2;

public static final int MODE_NIGHT_AUTO = 0;

public static final int MODE_NIGHT_FOLLOW_SYSTEM = -1;</pre>

其分別對應的是:不使用夜間模式,使用夜間模式,根據地理位置推算白天夜晚自動進入夜間或白天模式,根據Android系統設置的模式。

首先你需要在你的gradle文件中加入該版本的依賴包,然后修改主題為Theme.AppCompat.DayNight,同樣的你可以添加以night為后綴的value文件,例如:

定義完主題后,你可以在整個應用程序中設置默認值,比如:

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);

或者你可以通過setLocalNightMode方法,動態的設定主題。例如:

   mButtonDay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                recreate();
            }
        });
   mButtonNight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                recreate();
            }
        });

是不是很簡單!你也可以查看源碼 https://github.com/neuyu/android-support-new-features

Bottom Sheets

相信大部分同事都有過模仿ios的bottomsheet吧,現在Android的material design來了。

現在你僅僅需要將design包加入到你的項目:

compile 'com.android.support:design:23.2.0'

然后:

BottomSheetDialog dialog = new BottomSheetDialog(context);

View view = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_list, null);

RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.bottom_sheet_recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(context)); recyclerView.setAdapter(new SimpleAdapter()); dialog.setContentView(view); dialog.show();</pre>

其用法類似DialogFragment,需要自定義布局,具體的如何為其為其添加點擊事件等。通過附加一個BottomSheetBehavior到子視圖的CoordinatorLayout(添加app:layout_behavior =“android.support.design.widget.BottomSheetBehavior”),通過對BottomSheet的操作我們可以得到它的五種狀態。

STATE_COLLAPSED:關閉Bottom Sheets,高度可控屬性app:behavior_peekHeight(默認為0)

STATE_DRAGGING:在用戶直接拖動Bottom Sheets向上或向下的中間狀態

STATE_SETTLING:當Bottom Sheets被釋放和沉降到其最終位置記錄的狀態

STATE_EXPANDED:Bottom Sheets展開時的狀態

STATE_HIDDEN:默認情況下禁用(和啟用應用程序:behavior_hideable屬性),這使得用戶可以向下滑動Bottom Sheets完全隱藏底部。

支持矢量圖片和矢量圖片動畫

關于矢量圖和動畫以及bottom sheets的事件監聽會在github中以代碼的形式給出,歡迎大家fork和star,謝謝。

</div>

 本文由用戶 ufosaga 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!