最近在自定義Dialog時,發現網上提供的很多方案,最多的是FragmentDialog。
比如我們想要實現一個底部彈出的功能,雖然Google已推出官方版的BottomSheet,但是使用起來會發現點擊outsize區域功能沒有做,用起來很不方便,我們還是使用FragmentDialog自己實現吧
直接上代碼
首先是dialog布局文件pip_dialog_select
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_from_camera"
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="match_parent"
android:text="相機"/>
<Button
android:id="@+id/btn_from_gallery"
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="match_parent"
android:text="相冊"/>
</LinearLayout>
</LinearLayout>
FragmentDialog文件
public class BottomSelectDialog extends DialogFragment implements View.OnClickListener {
private View masker;
private LinearLayout layoutBottom;
private SelectFromListener selectFromListener;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//去掉dialog的標題,需要在setContentView()之前
this.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = this.getDialog().getWindow();
//去掉dialog默認的padding
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
//設置dialog的位置在底部
lp.gravity = Gravity.BOTTOM;
//設置dialog的動畫
lp.windowAnimations = R.style.BottomDialog;
window.setAttributes(lp);
window.setBackgroundDrawable(new ColorDrawable());
final View view = inflater.inflate(R.layout.pip_dialog_select, null);
view.findViewById(R.id.btn_from_camera).setOnClickListener(this);
view.findViewById(R.id.btn_from_gallery).setOnClickListener(this);
return view;
}
}
定義Dialog的主題,values/styls.xml
文件
<!-- 帶動畫的底部彈出dialog-->
<style name="BottomDialog" parent="android:Animation">
<item name="android:windowAnimationStyle">@style/BottomDialogAnimation</item>
</style>
<style name="BottomDialogAnimation">
<item name="android:windowEnterAnimation">@anim/dialog_slide_up</item>
<item name="android:windowExitAnimation">@anim/dialog_slide_down</item>
</style>
消失動畫文件res/anim/dialog_slid_down.xnl
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromYDelta="0"
android:toYDelta="100%p"/>
</set>
出現動畫文件res/anim/dialog_slid_up.xnl
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromYDelta="100%p"
android:toYDelta="0"/>
</set>
參照網上的寫法,基本上是這樣,但是會出現一個問題,如下:
1111.gif
發現彈出動畫根本沒起作用
查詢資料后發現Animation not working the in custom dialog
嘗試修改lp.windowAnimations
引用動畫為不引用Theme,直接引用Animation
修改前
lp.windowAnimations = R.style.BottomDialog
修改后
lp.windowAnimations = R.style.BottomDialogAnimation;
2222.gif
完美!
原因呢?待后續思考。。。