dialogFragment 会有弹出动画 创建有两种一种在oncreateView 一种在oncreateDialog,展示也有两种,一种是show会弹出对话框,还有一种是直接当作fragment内嵌在activity上
关于重复创建dialog: 我们都知道,如果我们点击一个按钮弹出一个对话框,如果每次都是新建Dialog ,则会出现重叠现象,在FragmentManager中有一个方法isAdded()可以用来判断此 Dialog是否被添加,同时,为了减少相同Dialog 的创建,我们并不需要每次都new 一个出来,并通过isAdded()来判断是否添加,减少不必要的消耗
public class BottomShareFragment extends DialogFragment implements View.OnClickListener {
/**
* 外界展示分享弹窗调用这个方法
*
* @param shareParamsBean 需要的总的分享参数
*/
public static BottomShareFragment showDialog(Activity activity) {
FragmentManager fragmentManager = activity.getFragmentManager();
BottomShareFragment bottomDialogFragment = (BottomShareFragment) fragmentManager.findFragmentByTag(TAG);
if (null == bottomDialogFragment) {
bottomDialogFragment = newInstance();
}
// 防止重复弹出
if (!activity.isFinishing() && null != bottomDialogFragment && !bottomDialogFragment.isAdded()) {
fragmentManager.beginTransaction().add(bottomDialogFragment, TAG).commitAllowingStateLoss();
}
return bottomDialogFragment;
}
/**
* 设置为单例
*/
private static BottomShareFragment newInstance() {
Bundle args = new Bundle();
BottomShareFragment fragment = new BottomShareFragment();
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.community_share_bottomshare_fragment, container, false);
initView(view);
return view;
}
/**
* 设置弹窗的展示格式
*/
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Window window = getDialog().getWindow();
if (window != null) {
// 一定要设置Background,如果不设置,window属性设置无效
window.setBackgroundDrawable(
new ColorDrawable(getResources().getColor(R.color.community_share_window_background)));
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM;
wlp.alpha = 0.8f;
wlp.dimAmount = 0.0f;// 设置灰度,这样别的地方才不会变暗
// 使用ViewGroup.LayoutParams,以便Dialog 宽度充满整个屏幕
wlp.width = ViewGroup.LayoutParams.MATCH_PARENT;
wlp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
window.setWindowAnimations(R.style.community_share_bottom_to_top_anim);
window.setAttributes(wlp);
}
}
}
=====================================layout=====================
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true">
<View
android:id="@+id/alpha_click_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/pop_share_window"/>
<LinearLayout
android:id="@+id/pop_share_window"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/community_shape_feed_detai_comment_bg"
android:orientation="vertical"
android:paddingBottom="60dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="58dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="17dp"
android:layout_marginStart="15dp"
android:text="@string/community_share_share"
android:textColor="@color/community_share_fragment_text_color"
android:textSize="29sp"
android:textStyle="bold">
</TextView>
<ImageView
android:id="@+id/community_share_cross_iv"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="12dp"
android:background="@drawable/community_share_cross_white_ic"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="15dp">
<ImageView
android:id="@+id/community_share_ins_iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:background="@drawable/community_share_share_ins_ic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/community_share_ins_iv"
android:layout_centerHorizontal="true"
android:text="@string/community_share_ins"
android:textColor="@color/community_share_fragment_text_color"
android:textSize="14sp"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="147dp"
android:layout_marginTop="15dp">
<ImageView
android:id="@+id/community_share_facebook_iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:background="@drawable/community_share_share_facebook_ic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/community_share_facebook_iv"
android:layout_centerHorizontal="true"
android:text="@string/community_share_facebook"
android:textColor="@color/community_share_fragment_text_color"
android:textSize="14sp"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="264dp"
android:layout_marginTop="15dp">
<ImageView
android:id="@+id/community_share_youtube_iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:background="@drawable/community_share_youtube_ic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/community_share_youtube_iv"
android:layout_centerHorizontal="true"
android:text="@string/community_share_youtube"
android:textColor="@color/community_share_fragment_text_color"
android:textSize="14sp"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="125dp">
<ImageView
android:id="@+id/community_share_whats_app_iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:background="@drawable/community_share_share_whats_app_ic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/community_share_whats_app_iv"
android:layout_centerHorizontal="true"
android:text="@string/community_share_whats_app"
android:textColor="@color/community_share_fragment_text_color"
android:textSize="14sp"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="147dp"
android:layout_marginTop="125dp">
<ImageView
android:id="@+id/community_share_twitter_iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:background="@drawable/community_share_share_twitter_ic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/community_share_twitter_iv"
android:layout_centerHorizontal="true"
android:text="@string/community_share_twitter"
android:textColor="@color/community_share_fragment_text_color"
android:textSize="14sp"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="264dp"
android:layout_marginTop="125dp">
<ImageView
android:id="@+id/community_share_email_iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/community_share_share_email_ic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/community_share_email_iv"
android:layout_centerHorizontal="true"
android:text="@string/community_share_email"
android:textColor="@color/community_share_fragment_text_color"
android:textSize="14sp"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="147dp"
android:layout_marginTop="235dp">
<ImageView
android:id="@+id/community_share_delete_iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:background="@drawable/community_share_share_delete_ic"/>
<TextView
android:id="@+id/community_share_delete_text_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/community_share_delete_iv"
android:layout_centerHorizontal="true"
android:text="@string/community_share_delete"
android:textColor="@color/community_share_fragment_text_color"
android:textSize="14sp"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="235dp">
<ImageView
android:id="@+id/community_share_message_iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:background="@drawable/community_share_message_ic"/>
<TextView
android:id="@+id/community_share_message_text_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/community_share_message_iv"
android:layout_centerHorizontal="true"
android:text="@string/community_share_message_text"
android:textColor="@color/community_share_fragment_text_color"
android:textSize="14sp"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="147dp"
android:layout_marginTop="235dp">
<ImageView
android:id="@+id/community_share_report_iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:background="@drawable/community_share_share_delete_ic"
android:visibility="gone"
/>
<TextView
android:id="@+id/community_share_report_text_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/community_share_report_iv"
android:layout_centerHorizontal="true"
android:text="@string/community_share_report"
android:textColor="@color/community_share_fragment_text_color"
android:textSize="14sp"
android:visibility="gone"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="264dp"
android:layout_marginTop="235dp">
<ImageView
android:id="@+id/community_share_save_iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:background="@drawable/community_share_share_save_ic"
/>
<TextView
android:id="@+id/community_share_save_text_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/community_share_save_iv"
android:layout_centerHorizontal="true"
android:text="@string/community_share_save"
android:textColor="@color/community_share_fragment_text_color"
android:textSize="14sp"
/>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
=============================================圆角背景==========================================
community_shape_feed_detai_comment_bg
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/framework_color_black80" />
<corners
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>