Android 自定義彈出菜單和對(duì)話框功能實(shí)例代碼
Android 開發(fā)當(dāng)中,可能會(huì)存在許多自定義布局的需求,比如自定義彈出菜單(popupWindow),以及自定義對(duì)話框(Dialog)。
話不多說(shuō),直接上圖片。

先講第一種,自定義PopUpWindow
1.popupWindow
protected void showPopWindow(View view, final int pos){
WindowManager wm= (WindowManager) myContext.getSystemService(Context.WINDOW_SERVICE);
int width=wm.getDefaultDisplay().getWidth();
LayoutInflater layoutInflater=(LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popView=layoutInflater.inflate(R.layout.layout_shoucang_popupwindow,null);
//加載彈出菜單的布局文件
final ListView lvpop= (ListView) popView.findViewById(R.id.lvShouCangPop);
List<String> strData=new ArrayList<>();
strData.add("刪除");
strData.add("分享");
popView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
popupWindow=new PopupWindow(popView,3*width/10, ViewGroup.LayoutParams.WRAP_CONTENT); //設(shè)置popupWindow 的大小
lvpop.setAdapter(new AdapterShouCangDeletePop(myContext,strData));
lvpop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0){
//點(diǎn)擊刪除按鈕的邏輯
// ToastUtil.toastButtom(myContext,"點(diǎn)擊刪除按鈕");
// datas.remove(pos); //remove掉這行數(shù)據(jù)
toActivityPos=pos;
// notifyDataSetChanged();
sendDeleteBoardCast(); //發(fā)送一條廣播
popupWindow.dismiss();
}else if(position ==1){
//點(diǎn)擊分享的邏輯
String title=datas.get(position).ucDesc;
String photoUrl=datas.get(position).ucIcon;
String contentUrl=datas.get(position).ucUrl;
DialogShouCangShare dialogShouCangShare=new DialogShouCangShare(myContext,title,photoUrl,contentUrl); //彈出分享對(duì)話框
dialogShouCangShare.show();
popupWindow.dismiss();
}
}
});
int[] location=new int[2];
view.getLocationOnScreen(location);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());//最好加上這一句,因?yàn)樗梢匀∠@示這個(gè)彈出菜單,不加的話,彈出菜單很難消失
//下方:popupWindow.showAsDropDown(v);
//popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]+v.getWidth(), location[1]); 顯示在右邊
//popupWindow顯示在左邊
popupWindow.showAtLocation(view, Gravity.NO_GRAVITY
, location[0]-popupWindow.getWidth(),location[1]); //這里的view是傳進(jìn)來(lái)的view,比如點(diǎn)擊事件中的view,就把它傳進(jìn)來(lái),popupwindow的位置可以自行調(diào)整
}
彈出菜單的布局,用listView 填充,然后由于要加圓角的背景,所以更改background
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/lvShouCangPop" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="2dp" android:background="@drawable/bg_shoucang_popup_bg" android:listSelector="@drawable/izd_shoucang_delete_selector_pop" /> </LinearLayout>
listView的圓角背景圖片
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="#eeeeee"/> <corners android:radius="8.0dip"/> </shape> </item> </selector>
然后你只要在你的邏輯代碼中調(diào)用showPopWindow()這個(gè)方法就行了,是不是很簡(jiǎn)單!
緊接著開始講自定義對(duì)話框了,因?yàn)楹芏郺pp中都有這個(gè)功能,而且效果還不錯(cuò)!
public class DialogShouCangShare extends Dialog{
private Context myContext;
private RelativeLayout rlCancle;
private GridView gridView;
//那些圖片
private int[] data=new int[]{R.drawable.izd_shoucang_wechat,R.drawable.izd_shoucang_friend,R.drawable.izd_shoucang_qq,
R.drawable.izd_shoucang_weibo,R.drawable.izd_shoucang_qzone,R.drawable.izd_shoucang_email};
public DialogShouCangShare(Context context,String title,String photoUrl,String contentUrl) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.izd_shoucang_dialog_share);
ShareSDK.initSDK(myContext);
gridView = (GridView) super.findViewById(R.id.gv_share);
gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
AdapterSCShareGridView adapter=new AdapterSCShareGridView(myContext,data);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position) {
//對(duì)于GridView中的item的點(diǎn)擊事件
}
DialogShouCangShare.this.dismiss();
}
});
rlCancle = (RelativeLayout) findViewById(R.id.shoucang_rlCancle);
rlCancle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogShouCangShare.this.dismiss();
}
});
@Override
public void show()
{
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
this.setCanceledOnTouchOutside(true);
Window dialogWindow = this.getWindow(); //得到對(duì)話框
dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialogWindow.setAttributes(lp);
dialogWindow.setWindowAnimations(R.style.izd_dialogWindowAnim); //設(shè)置窗口彈出動(dòng)畫 ,由styles配置,有進(jìn)入和退出動(dòng)畫
//dialogWindow.setWindowAnimations(R.anim.dialog_enter_anim);
//
// WindowManager.LayoutParams lp = dialogWindow.getAttributes();
// lp.width = 100; // 寬度
// lp.height = 300; // 高度
// //lp.alpha = 0.7f; // 透明度
// //dialogWindow.setAttributes(lp);
dialogWindow.setBackgroundDrawableResource(R.drawable.radius_shoucang_share_nopadding); //設(shè)置對(duì)話框背景
// dialogWindow.setBackgroundDrawableResource(R.color.izd_white); //設(shè)置對(duì)話框背景
super.show();
}
}
再看下該對(duì)話框的布局文件:只有一個(gè)gridView 和relativeLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="8dp"
android:background="@color/transparent"
>
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<GridView
android:id="@+id/gv_share"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:numColumns="3"
android:verticalSpacing="-36dp"
android:background="@drawable/bg_share_shoucang">
</GridView>
<RelativeLayout
android:id="@+id/shoucang_rlCancle"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="8dp"
android:background="@drawable/bg_share_shoucang"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
android:textColor="#009688"
android:textSize="16sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
這是設(shè)置對(duì)話框的背景的布局文件,其實(shí)主要設(shè)置對(duì)話框的圓角,以及對(duì)話框顏色為透明就行了!
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ffffff"/> <corners android:radius="4dp" /> <gradient android:startColor="#00000000" android:endColor="#00000000"/> </shape>
再次聲明,這里使用GridView是為了,方便以后填充更多的數(shù)據(jù),如果用相對(duì)布局加線性布局,寫死的話,以后若要再次添加數(shù)據(jù)的話,就要再去修改布局,比較麻煩!因?yàn)橛星败囍b的我,下面就是我之前不用GridView去寫的布局文件!新手如果想練手的話,可以嘗試!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:src="@drawable/izd_shoucang_wechat"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="微信"
android:layout_gravity="center"
android:layout_marginTop="9dp"
android:layout_centerHorizontal="true"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:src="@drawable/izd_shoucang_friend"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="朋友圈"
android:layout_gravity="center"
android:layout_marginTop="9dp"
android:layout_centerHorizontal="true"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:src="@drawable/izd_shoucang_qq"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QQ好友"
android:layout_gravity="center"
android:layout_marginTop="9dp"
android:layout_centerHorizontal="true"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:src="@drawable/izd_shoucang_weibo"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="微博"
android:layout_gravity="center"
android:layout_marginTop="9dp"
android:layout_centerHorizontal="true"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:src="@drawable/izd_shoucang_qzone"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QQ空間"
android:layout_gravity="center"
android:layout_marginTop="9dp"
android:layout_centerHorizontal="true"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:src="@drawable/izd_shoucang_email"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="郵箱"
android:layout_gravity="center"
android:layout_marginTop="9dp"
android:layout_centerHorizontal="true"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="8dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
android:textColor="#009688"
android:textSize="16sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
</LinearLayout>
效果也是一樣的!

然后你要使用該對(duì)話框的話,只要新建對(duì)話框就可以了!
DialogShouCangShare dialogShouCangShare=new DialogShouCangShare(myContext); //彈出分享對(duì)話框 dialogShouCangShare.show();
總結(jié)
以上所述是小編給大家介紹的Android 自定義彈出菜單和對(duì)話框功能實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android開發(fā)技巧之我的菜單我做主(自定義菜單)
- android自定義popupwindow仿微信右上角彈出菜單效果
- Android實(shí)現(xiàn)自定義滑動(dòng)式抽屜菜單效果
- android 自定義Android菜單背景的代碼
- Android自定義ViewGroup實(shí)現(xiàn)帶箭頭的圓角矩形菜單
- Android自定義view實(shí)現(xiàn)圓形與半圓形菜單
- Android實(shí)現(xiàn)自定義的衛(wèi)星式菜單(弧形菜單)詳解
- Android編程自定義菜單實(shí)現(xiàn)方法詳解
- Android自定義控件簡(jiǎn)單實(shí)現(xiàn)側(cè)滑菜單效果
- Android自定義View展開菜單功能的實(shí)現(xiàn)
- Android使用xml文件資源定義菜單實(shí)現(xiàn)方法示例
相關(guān)文章
python只需30行代碼就能記錄鍵盤的一舉一動(dòng)
這篇文章主要介紹了如何用python只寫30行代碼就能記錄鍵盤的一舉一動(dòng),感興趣的同學(xué)快來(lái)看看吧,新手小白也能掌握2021-08-08
Android編程中號(hào)碼匹配位數(shù)修改的方法
這篇文章主要介紹了Android編程中號(hào)碼匹配位數(shù)修改的方法,涉及Android編程中參數(shù)修改的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android studio實(shí)現(xiàn)菜單效果
這篇文章主要為大家詳細(xì)介紹了Android studio實(shí)現(xiàn)菜單效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Android開發(fā)實(shí)現(xiàn)調(diào)節(jié)屏幕亮度功能
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)調(diào)節(jié)屏幕亮度功能,涉及Android權(quán)限控制及屏幕亮度相關(guān)屬性操作技巧,需要的朋友可以參考下2018-03-03
Android自定義收音機(jī)搜臺(tái)控件RadioRulerView
這篇文章主要為大家詳細(xì)介紹了Android自定義收音機(jī)搜臺(tái)控件RadioRulerView的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Android中使用SQLite3 命令行查看內(nèi)嵌數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Android中使用SQLite3 命令行查看內(nèi)嵌數(shù)據(jù)庫(kù)的方法的相關(guān)資料,需要的朋友可以參考下2015-12-12
詳解Android?Flutter如何自定義動(dòng)畫路由
flutter中有默認(rèn)的Route組件,叫做MaterialPageRoute,但是MaterialPageRoute太普通了,如果我們想要做點(diǎn)不同的跳轉(zhuǎn)特效應(yīng)該如何處理呢?一起來(lái)看看吧2023-04-04
Android編程實(shí)現(xiàn)TextView部分顏色變動(dòng)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)TextView部分顏色變動(dòng)的方法,實(shí)例分析了TextView設(shè)置指定位置的背景色與字體顏色的相關(guān)技巧,需要的朋友可以參考下2015-12-12
Android利用shape實(shí)現(xiàn)各種簡(jiǎn)單的形狀
這篇文章主要給大家介紹了關(guān)于Android中利用shape實(shí)現(xiàn)各種簡(jiǎn)單的形狀的相關(guān)資料,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-05-05
android 線性布局LinearLayout實(shí)例代碼
android 線性布局LinearLayout實(shí)例代碼,需要的朋友可以參考一下2013-05-05

