官方推薦我們使用FragmentDialog來(lái)統(tǒng)一風(fēng)格铺罢,使界面更美觀历葛!
于是推出了AppCompatDialog,AppCompatDialogFragment茴晋,而B(niǎo)ottomSheetDialogFragment就是繼承它而來(lái)的,我們要在此基礎(chǔ)上再次封裝一下回窘,以適應(yīng)我們自己的需求
- 首先我們要引入相關(guān)的兼容包诺擅,版本號(hào)就看你自己的環(huán)境了
compile 'com.android.support:design:26.0.0-alpha1'
- 我們這里還需要重寫(xiě)一下BottomSheetDialog
解決使用BottomSheetDialog時(shí)狀態(tài)欄變黑的問(wèn)題
public class BottomSheetDialog extends android.support.design.widget.BottomSheetDialog{
public BottomSheetDialog(@NonNull Context context) {
super(context);
BottomSheetDialogFragment dd;
}
public BottomSheetDialog(@NonNull Context context, @StyleRes int theme) {
super(context, theme);
}
protected BottomSheetDialog(@NonNull Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int screenHeight = getScreenHeight(getOwnerActivity());
int statusBarHeight = getStatusBarHeight(getContext());
int dialogHeight = screenHeight - statusBarHeight;
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, dialogHeight == 0 ? ViewGroup.LayoutParams.MATCH_PARENT : dialogHeight);
}
private static int getScreenHeight(Activity activity) {
DisplayMetrics displaymetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
return displaymetrics.heightPixels;
}
private static int getStatusBarHeight(Context context) {
int statusBarHeight = 0;
Resources res = context.getResources();
int resourceId = res.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = res.getDimensionPixelSize(resourceId);
}
return statusBarHeight;
}
}
- 然后寫(xiě)我們自定義的BottomMenuDialog
public class BottomMenuDialog extends BottomSheetDialogFragment implements View.OnClickListener {
public static final String TAG = "BOTTOM_MENU";
//必要參數(shù) 標(biāo)題集合
private ArrayList<String> titles;
//必要參數(shù) 監(jiān)聽(tīng)器集合
private Map<String, View.OnClickListener> listeners;
//菜單文字顏色
private int textColor = Color.parseColor("#55C1FF");
private LinearLayout rootView;
protected Dialog dialog;
private Context mContext;
protected BottomSheetBehavior mBehavior;
public BottomMenuDialog() {
}
@SuppressLint("ValidFragment")
public BottomMenuDialog(BottomMenuBuilder builder) {
titles = builder.titles;
listeners = builder.listeners;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.mContext = context;
}
@Override
public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
WindowManager.LayoutParams windowParams = window.getAttributes();
//這里設(shè)置透明度
windowParams.dimAmount = 0.5f;
window.setAttributes(windowParams);
}
@Override
public void onDestroy() {
super.onDestroy();
//解除緩存View和當(dāng)前ViewGroup的關(guān)聯(lián)
((ViewGroup) (rootView.getParent())).removeView(rootView);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
dialog = new BottomSheetDialog(getContext(), getTheme());
if (rootView == null) {
//緩存下來(lái)的View 當(dāng)為空時(shí)才需要初始化 并緩存
initRootView();
}
dialog.setContentView(rootView);
mBehavior = BottomSheetBehavior.from((View) rootView.getParent());
((View) rootView.getParent()).setBackgroundColor(Color.TRANSPARENT);
rootView.post(new Runnable() {
@Override
public void run() {
/**
* PeekHeight默認(rèn)高度256dp 會(huì)在該高度上懸浮
* 設(shè)置等于view的高 就不會(huì)卡住
*/
mBehavior.setPeekHeight(rootView.getHeight());
}
});
return dialog;
}
private void initRootView() {
rootView = new LinearLayout(mContext);
rootView.setOrientation(LinearLayout.VERTICAL);
rootView.setPadding(dp2px(mContext, 10), 0, dp2px(mContext, 10), dp2px(mContext, 10));
if (titles == null || titles.size() == 0) {
return;
}
if (titles.size() == 1) {
View childView = initView(titles.get(0), 1, false, false);
childView.setBackgroundDrawable(getDrawableListByType(true, true, true, true));
return;
}
if (titles.size() == 2) {
View childView = initView(titles.get(0), 1, false, true);
childView.setBackgroundDrawable(getDrawableListByType(true, true, true, true));
childView = initView(titles.get(1), 2, false, false);
childView.setBackgroundDrawable(getDrawableListByType(true, true, true, true));
return;
}
for (int i = 0; i < titles.size(); i++) {
View childView = null;
if (i == 0) {
childView = initView(titles.get(0), i + 1, true, false);
childView.setBackgroundDrawable(getDrawableListByType(true, true, false, false));
continue;
}
if (i == (titles.size() - 2)) {
childView = initView(titles.get(i), i + 1, false, true);
childView.setBackgroundDrawable(getDrawableListByType(false, false, true, true));
continue;
}
if (i == (titles.size() - 1)) {
//false, false, true, true
childView = initView(titles.get(i), i + 1, false, false);
childView.setBackgroundDrawable(getDrawableListByType(true, true, true, true));
continue;
}
childView = initView(titles.get(i), i + 1, true, false);
childView.setBackgroundDrawable(getDrawableListByType(false, false, false, false));
}
}
private View initView(String button, int position, boolean hasBottomLine, boolean hasBottomGap) {
TextView childView = new TextView(mContext);
childView.setText(button);
childView.setTextSize(18);
childView.setTextColor(textColor);
childView.setTag(position);
childView.setGravity(Gravity.CENTER);
childView.setOnClickListener(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dp2px(mContext, 50));
if (hasBottomGap) params.bottomMargin = dp2px(mContext, 10);
rootView.addView(childView, params);
if (hasBottomLine) {
View line = new View(mContext);
line.setBackgroundColor(Color.LTGRAY);
rootView.addView(line, LinearLayout.LayoutParams.MATCH_PARENT, 1);
}
return childView;
}
public void setTextColor(int color) {
this.textColor = color;
}
public StateListDrawable getDrawableListByType(boolean leftTop, boolean rightTop, boolean rightBottom, boolean leftBottom) {
StateListDrawable stateListDrawable = new StateListDrawable();
Drawable selectDrawable = getWhitShape(5, 0xffCCCCCC, leftTop, rightTop, rightBottom, leftBottom);
Drawable defaultDrawable = getWhitShape(5, 0xffffffff, leftTop, rightTop, rightBottom, leftBottom);
stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, selectDrawable);
stateListDrawable.addState(new int[]{}, defaultDrawable);
return stateListDrawable;
}
public Drawable getWhitShape(int radius, int bgColor, boolean leftTop, boolean rightTop, boolean rightBottom, boolean leftBottom) {
float r = dp2px(getContext(), radius);
float a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0, a6 = 0, a7 = 0, a8 = 0;
if (leftTop) {
a1 = r;
a2 = r;
}
if (rightTop) {
a3 = r;
a4 = r;
}
if (rightBottom) {
a5 = r;
a6 = r;
}
if (leftBottom) {
a7 = r;
a8 = r;
}
float[] outerRadii = new float[]{a1, a2, a3, a4, a5, a6, a7, a8};
RoundRectShape rrs = new RoundRectShape(outerRadii, null, null);
ShapeDrawable sd = new ShapeDrawable(rrs);
sd.getPaint().setColor(bgColor);
return sd;
}
public void show(FragmentManager manager) {
if (!this.isAdded())
show(manager, TAG);
}
public static int dp2px(Context context, float dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, context.getResources().getDisplayMetrics());
}
@Override
public void onClick(View v) {
Object o = v.getTag();
if (o == null) {
return;
}
String key = String.valueOf(o);
if (listeners.get(key) != null) {
listeners.get(key).onClick(v);
}
dismiss();
}
public static class BottomMenuBuilder {
//必要參數(shù) 標(biāo)題集合
private ArrayList<String> titles;
//必要參數(shù) 監(jiān)聽(tīng)器集合
private Map<String, View.OnClickListener> listeners;
public BottomMenuBuilder() {
titles = new ArrayList<>();
listeners = new HashMap<>();
}
public BottomMenuDialog build() {
if (titles == null || titles.isEmpty()) {
L.e(TAG, "can not empty titles");
}
return new BottomMenuDialog(this);
}
public BottomMenuBuilder addItem(String title, View.OnClickListener listener) {
titles.add(title);
listeners.put(String.valueOf(titles.size()), listener);
return this;
}
}
}
- 使用
new BottomMenuDialog.BottomMenuBuilder()
.addItem("相機(jī)", new View.OnClickListener() {
@Override
public void onClick(View v) {
}
})
.addItem("相冊(cè)", new View.OnClickListener() {
@Override
public void onClick(View v) {
}
})
.addItem("取消",null)
.build().show(getSupportFragmentManager());
最好不要添加過(guò)多數(shù)據(jù),撐滿屏幕是不會(huì)擴(kuò)展的啡直,最后看效果:
image.png
更多功能就需要我們自己探索了烁涌!