您好,登錄后才能下訂單哦!
Android 開(kāi)發(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)單!
緊接著開(kāi)始講自定義對(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)畫(huà) ,由styles配置,有進(jìn)入和退出動(dòng)畫(huà) //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ì)布局加線性布局,寫(xiě)死的話,以后若要再次添加數(shù)據(jù)的話,就要再去修改布局,比較麻煩!因?yàn)橛星败囍b的我,下面就是我之前不用GridView去寫(xiě)的布局文件!新手如果想練手的話,可以嘗試!
<?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)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。