版權聲明:本文爲HaiyuKing原創文章,轉載請註明出處!java
整理項目中常常用到的分享對話框的實現。android
這裏只是一個分享對話框,不涉及分享功能的實現。git
ShareDialog.java:分享對話框,繼承DialogFragment【按常理來說,應該繼承BottomSheetDialog,可是考慮到可能須要請求接口獲取分享縮略圖,因此繼承DialogFragment】github
注意事項:微信
一、 導入類文件後須要change包名以及從新import R文件路徑app
二、 Values目錄下的文件(strings.xml、dimens.xml、colors.xml等),若是項目中存在,則複製裏面的內容,不要整個覆蓋ide
package com.why.project.sharedialogdemo.dialog; import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.util.DisplayMetrics; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.TextView; import com.why.project.sharedialogdemo.R; /** * Used 分享底部對話框【按常理來說,應該extends BottomSheetDialog】 * 可是,由於這個分享對話框有可能須要請求接口,因此最終仍是繼承DialogFragment */ public class ShareDialog extends DialogFragment { private static final String TAG = ShareDialog.class.getSimpleName(); /**View實例*/ private View myView; /**context實例*/ private Context mContext; /**標記:用來表明是從哪一個界面打開的這個對話框*/ private String mTag; /**QQ分享*/ private TextView shareQQ; /**QQ空間分享*/ private TextView shareQZone; /**微信分享*/ private TextView shareWX; /**朋友圈分享*/ private TextView shareWXCircle; /**新浪微博分享*/ private TextView shareSina; public static ShareDialog getInstance(Context mContext, Bundle bundle) { ShareDialog shareDialog = new ShareDialog(); shareDialog.mContext = mContext; return shareDialog; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));//設置背景爲透明,而且沒有標題 getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉標題欄,不然點擊正上方必定高度的範圍內不會消失 myView = inflater.inflate(R.layout.dialog_share, container, false); return myView; } @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); //初始化控件以及設置 initView(); //初始化數據 initDatas(); //初始化事件 initEvents(); } /** * 設置寬度和高度值,以及打開的動畫效果 */ @Override public void onStart() { super.onStart(); //設置對話框的寬高,必須在onStart中 DisplayMetrics metrics = new DisplayMetrics(); this.getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics); Window window = this.getDialog().getWindow(); window.setLayout(metrics.widthPixels, this.getDialog().getWindow().getAttributes().height); window.setGravity(Gravity.BOTTOM);//設置在底部 //打開的動畫效果 //設置dialog的 進出 動畫 getDialog().getWindow().setWindowAnimations(R.style.sharebottomsheetdialog_animation); } /**實例化控件*/ private void initView() { shareQQ = (TextView) myView.findViewById(R.id.share_qq); shareQZone = (TextView) myView.findViewById(R.id.share_qzone); shareWX = (TextView) myView.findViewById(R.id.share_weixin); shareWXCircle = (TextView) myView.findViewById(R.id.share_wxcircle); shareSina = (TextView) myView.findViewById(R.id.share_sina); } /** * 初始化數據:tag標記、標題 */ private void initDatas() { mTag = this.getTag(); Log.e(TAG, "mTag=" + mTag); } /** * 初始化監聽事件 */ private void initEvents() { //QQ分享 shareQQ.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); if(mOnShareClickLitener != null){ mOnShareClickLitener.onShareToQQ(); } } }); //QQ空間分享 shareQZone.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); if(mOnShareClickLitener != null){ mOnShareClickLitener.onShareToQZone(); } } }); //微信分享 shareWX.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); if(mOnShareClickLitener != null){ mOnShareClickLitener.onShareToWX(); } } }); //朋友圈分享 shareWXCircle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); if(mOnShareClickLitener != null){ mOnShareClickLitener.onShareToWXCircle(); } } }); //新浪微博分享 shareSina.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); if(mOnShareClickLitener != null){ mOnShareClickLitener.onShareToSina(); } } }); } /*=====================添加OnShareClickLitener回調================================*/ public interface OnShareClickLitener { void onShareToQQ(); void onShareToQZone(); void onShareToWX(); void onShareToWXCircle(); void onShareToSina(); } private OnShareClickLitener mOnShareClickLitener; public void setOnShareClickLitener(OnShareClickLitener mOnShareClickLitener) { this.mOnShareClickLitener = mOnShareClickLitener; } }
<?xml version="1.0" encoding="utf-8"?> <!-- 分享對話框佈局文件 --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/share_root" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#EFEFF4" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:layout_marginBottom="15dp" android:orientation="horizontal" > <!-- QQ分享 --> <TextView android:id="@+id/share_qq" android:layout_width="0.0dp" android:layout_weight="1" android:layout_height="wrap_content" android:adjustViewBounds="true" android:drawableTop="@drawable/share_logo_qq" android:drawablePadding="5dp" android:text="@string/share_qq" android:textSize="14sp" android:gravity="center" /> <!-- QQ空間分享 --> <TextView android:id="@+id/share_qzone" android:layout_width="0.0dp" android:layout_weight="1" android:layout_height="wrap_content" android:adjustViewBounds="true" android:drawableTop="@drawable/share_logo_qq_friend" android:drawablePadding="5dp" android:text="@string/share_qzone" android:textSize="14sp" android:gravity="center" /> <!-- 微信分享 --> <TextView android:id="@+id/share_weixin" android:layout_width="0.0dp" android:layout_weight="1" android:layout_height="wrap_content" android:adjustViewBounds="true" android:drawableTop="@drawable/share_logo_weixin" android:drawablePadding="5dp" android:text="@string/share_wx" android:textSize="14sp" android:gravity="center" /> <!-- 朋友圈分享 --> <TextView android:id="@+id/share_wxcircle" android:layout_width="0.0dp" android:layout_weight="1" android:layout_height="wrap_content" android:adjustViewBounds="true" android:drawableTop="@drawable/share_logo_weixin_friend" android:drawablePadding="5dp" android:text="@string/share_wx_circle" android:textSize="14sp" android:gravity="center" /> <!-- 新浪微博分享 --> <TextView android:id="@+id/share_sina" android:layout_width="0.0dp" android:layout_weight="1" android:layout_height="wrap_content" android:adjustViewBounds="true" android:drawableTop="@drawable/share_logo_sina" android:drawablePadding="5dp" android:text="@string/share_sina" android:textSize="14sp" android:gravity="center" /> </LinearLayout> </RelativeLayout>
<resources> <string name="app_name">ShareDialogDemo</string> <!-- 分享對話框 --> <string name="share_qq">QQ</string> <string name="share_qzone">QQ空間</string> <string name="share_wx">微信</string> <string name="share_wx_circle">朋友圈</string> <string name="share_sina">新浪</string> </resources>
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <!-- 分享對話框動畫 --> <style name="sharebottomsheetdialog_animation"> <item name="android:windowEnterAnimation">@anim/sharebottomsheetdialog_bottom_in</item> <item name="android:windowExitAnimation">@anim/sharebottomsheetdialog_bottom_out</item> </style> </resources>
至此,ShareDialog就集成到項目中了。佈局
package com.why.project.sharedialogdemo; import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Toast; import com.why.project.sharedialogdemo.dialog.ShareDialog; public class MainActivity extends AppCompatActivity { private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this; findViewById(R.id.btn_open).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //顯示分享底部區域 Bundle bundle = new Bundle(); ShareDialog shareDialog = ShareDialog.getInstance(mContext,bundle); shareDialog.setOnShareClickLitener(new ShareDialog.OnShareClickLitener() { @Override public void onShareToQQ() { openShare("QQ"); } @Override public void onShareToQZone() { openShare("QZone"); } @Override public void onShareToWX() { openShare("WX"); } @Override public void onShareToWXCircle() { openShare("WXCircle"); } @Override public void onShareToSina() { openShare("Sina"); } }); shareDialog.show(getSupportFragmentManager(), "Share"); } }); } //在這裏能夠配合友盟分享,經過switch語句,根據type判斷平臺。執行分享代碼 private void openShare(String type){ Toast.makeText(mContext, type, Toast.LENGTH_SHORT).show(); } }
無動畫
暫時空缺this