最近在自定義Dialog時(shí),發(fā)現(xiàn)網(wǎng)上提供的很多方案毡代,最多的是FragmentDialog阅羹。
比如我們想要實(shí)現(xiàn)一個(gè)底部彈出的功能,雖然Google已推出官方版的BottomSheet教寂,但是使用起來(lái)會(huì)發(fā)現(xiàn)點(diǎn)擊outsize區(qū)域功能沒(méi)有做捏鱼,用起來(lái)很不方便,我們還是使用FragmentDialog自己實(shí)現(xiàn)吧
直接上代碼
首先是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="相機(jī)"/>
<Button
android:id="@+id/btn_from_gallery"
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="match_parent"
android:text="相冊(cè)"/>
</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的標(biāo)題酪耕,需要在setContentView()之前
this.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = this.getDialog().getWindow();
//去掉dialog默認(rèn)的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;
//設(shè)置dialog的位置在底部
lp.gravity = Gravity.BOTTOM;
//設(shè)置dialog的動(dòng)畫(huà)
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
文件
<!-- 帶動(dòng)畫(huà)的底部彈出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>
消失動(dòng)畫(huà)文件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>
出現(xiàn)動(dòng)畫(huà)文件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>
參照網(wǎng)上的寫(xiě)法,基本上是這樣因妇,但是會(huì)出現(xiàn)一個(gè)問(wèn)題问潭,如下:
1111.gif
發(fā)現(xiàn)彈出動(dòng)畫(huà)根本沒(méi)起作用
查詢資料后發(fā)現(xiàn)Animation not working the in custom dialog
嘗試修改lp.windowAnimations
引用動(dòng)畫(huà)為不引用Theme,直接引用Animation
修改前
lp.windowAnimations = R.style.BottomDialog
修改后
lp.windowAnimations = R.style.BottomDialogAnimation;
2222.gif
完美婚被!
原因呢狡忙?待后續(xù)思考。址芯。灾茁。