实现此效果,直接上代码:
public class CustomFragment extends DialogFragment {
@Bind(R.id.close)
ImageView close;
@Bind(R.id.confirm)
Button confirm;
private SomethingItem item;
private SomethingItem tmpItem;
private boolean flag;
public static CustomFragment newInstance(DishTradeItem item, boolean flag) {
CustomFragment f = new CustomFragment();
Bundle bundle = new Bundle();
bundle.putSerializable("item", item);
bundle.putBoolean("flag", flag);
f.setArguments(bundle);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//样式设置
setStyle(DialogFragment.STYLE_NORMAL, R.style.dialog_fragment);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);//默认不弹出软键盘
getDialog().getWindow().setGravity(Gravity.BOTTOM);//Dialog处于页面的底部
getDialog().setCanceledOnTouchOutside(true);//点击Dialog外围可以消除Dialog
Bundle bundle = getArguments();
if (bundle != null) {
item = (SomethingItem) bundle.getSerializable("item");
flag = bundle.getBoolean("flag",false);
Gson gson = new Gson();
/*深拷贝一份,点击确定后才变化*/
tmpItem = gson.fromJson(gson.toJson(item), SomethingItem.class);
}
View root = inflater.inflate(R.layout.custom_layout, container, false);
ButterKnife.bind(this, root);
/**
*添加页面的初始化
**/
return root;
}
@Override
public void onResume() {
super.onResume();
DisplayMetrics metric = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metric);
int maxHeight = (int) (metric.heightPixels * 0.8);//暂定高度
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, maxHeight);
}
@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);
}
@OnClick(R.id.close)
public void closeDialog() {
dismissAllowingStateLoss();
}
@OnClick(R.id.confirm)
public void confirm() {
/**
*添加确认处理逻辑
**/
dismissAllowingStateLoss();
}
@Override
public void show(FragmentManager manager, String tag) {
//当正在显示时,不再新弹
if (manager.findFragmentByTag(tag) == null) {
super.show(manager, tag);
}
}
}
对应的style如下:
<style name="dialog_fragment_animation">
<item name="android:windowEnterAnimation">@anim/dialog_in_bottom</item>
<item name="android:windowExitAnimation">@anim/dialog_out_bottom</item>
</style>
<style name="dialog_fragment" parent="@android:style/Theme">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:layout_width">match_parent</item>
<item name="android:windowFullscreen">true</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:padding">0dp</item>
<item name="android:gravity">bottom</item>
<item name="android:windowSoftInputMode">stateAlwaysHidden|adjustResize</item>
<item name="android:windowAnimationStyle">@style/dialog_fragment_animation</item>
</style>
dialog_in_bottom.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="100%p" android:toYDelta="0%p"
android:duration="600"/>
</set>
dialog_out_bottom.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="100%p" android:toYDelta="0%p"
android:duration="600"/>
</set>