在Android中使用PopupWindow實(shí)現(xiàn)一個(gè)彈出分享功能?很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
肇慶網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)自2013年起到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
代碼
package com.duanlian.popupwindowdemo; import android.app.Activity; import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.TextView; /** * 自定義popupWindow */ public class RewritePopwindow extends PopupWindow { private View mView; public RewritePopwindow(Activity context, View.OnClickListener itemsOnClick) { super(context); initView(context, itemsOnClick); } private void initView(final Activity context, View.OnClickListener itemsOnClick) { LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mView = mInflater.inflate(R.layout.popupwindow_share, null); LinearLayout weiXFriend = (LinearLayout) mView.findViewById(R.id.weixinghaoyou); LinearLayout friendster = (LinearLayout) mView.findViewById(R.id.pengyouquan); LinearLayout QQFriend = (LinearLayout) mView.findViewById(R.id.qqhaoyou); LinearLayout QQZone = (LinearLayout) mView.findViewById(R.id.qqkongjian); TextView canaleTv = (TextView) mView.findViewById(R.id.share_cancle); canaleTv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //銷毀彈出框 dismiss(); backgroundAlpha(context, 1f); } }); //設(shè)置按鈕監(jiān)聽 weiXFriend.setOnClickListener(itemsOnClick); friendster.setOnClickListener(itemsOnClick); QQFriend.setOnClickListener(itemsOnClick); QQZone.setOnClickListener(itemsOnClick); //設(shè)置SelectPicPopupWindow的View this.setContentView(mView); //設(shè)置SelectPicPopupWindow彈出窗體的寬 this.setWidth(WindowManager.LayoutParams.FILL_PARENT); //設(shè)置SelectPicPopupWindow彈出窗體的高 this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); //設(shè)置SelectPicPopupWindow彈出窗體可點(diǎn)擊 this.setFocusable(true); //設(shè)置PopupWindow可觸摸 this.setTouchable(true); //設(shè)置非PopupWindow區(qū)域是否可觸摸 // this.setOutsideTouchable(false); //設(shè)置SelectPicPopupWindow彈出窗體動畫效果 // this.setAnimationStyle(R.style.select_anim); //實(shí)例化一個(gè)ColorDrawable顏色為半透明 ColorDrawable dw = new ColorDrawable(0x00000000); //設(shè)置SelectPicPopupWindow彈出窗體的背景 this.setBackgroundDrawable(dw); backgroundAlpha(context, 0.5f);//0.0-1.0 this.setOnDismissListener(new OnDismissListener() { @Override public void onDismiss() { // TODO Auto-generated method stub backgroundAlpha(context, 1f); } }); } /** * 設(shè)置添加屏幕的背景透明度 * * @param bgAlpha */ public void backgroundAlpha(Activity context, float bgAlpha) { WindowManager.LayoutParams lp = context.getWindow().getAttributes(); lp.alpha = bgAlpha; context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); context.getWindow().setAttributes(lp); } }
MainActivity中用
package com.duanlian.popupwindowdemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Gravity; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private RewritePopwindow mPopwindow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.share).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mPopwindow = new RewritePopwindow(MainActivity.this, itemsOnClick); mPopwindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); } }); } //為彈出窗口實(shí)現(xiàn)監(jiān)聽類 private View.OnClickListener itemsOnClick = new View.OnClickListener() { public void onClick(View v) { mPopwindow.dismiss(); mPopwindow.backgroundAlpha(MainActivity.this, 1f); switch (v.getId()) { case R.id.weixinghaoyou: Toast.makeText(MainActivity.this, "微信好友", Toast.LENGTH_SHORT).show(); break; case R.id.pengyouquan: Toast.makeText(MainActivity.this, "朋友圈", Toast.LENGTH_SHORT).show(); break; case R.id.qqhaoyou: Toast.makeText(MainActivity.this, "QQ好友", Toast.LENGTH_SHORT).show(); break; case R.id.qqkongjian: Toast.makeText(MainActivity.this, "QQ空間", Toast.LENGTH_SHORT).show(); break; default: break; } } }; }
布局文件--popupWindow布局
<?xml version="1.0" encoding="utf-8"?>
mainActivity的布局文件
<?xml version="1.0" encoding="utf-8"?>
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。