国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android自定義PopWindow實現QQ、微信彈出菜單效果

2019-10-22 18:12:31
字體:
來源:轉載
供稿:網友

前段時間在個人開發的項目中需要用到彈出菜單,類似QQ右上角的彈出菜單,自己使用popwin的次數也不是很多,其中也遇到過一點問題,今天正好有時間就把一些經驗分享給大家。

先來看看最終實現過后的效果怎么樣,下面放上圖

Android,PopWindow,QQ,微信

自定義的彈出菜單是繼承的popwin,并不是view 因為沒有必要重復造車輪,如果想要實現某種特殊的效果另說。首先創建類MyPopWindow繼承Popwindow。

public class MyPopWindow extends PopupWindow implements View.OnClickListener { private Context context; private View view; private LinearLayout scan; private LinearLayout add; public MyPopWindow(Context context) { this(context, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); } public MyPopWindow(Context context, int width, int height) { super(context); this.context = context; setWidth(width); setHeight(height); setFocusable(true); setOutsideTouchable(true); setTouchable(true); view = LayoutInflater.from(context).inflate(R.layout.layout_mypopwin,null); setContentView(view); scan = (LinearLayout) view.findViewById(R.id.scan); add = (LinearLayout) view.findViewById(R.id.add); } @Override public void onClick(View view) { switch (view.getId()){  case R.id.scan:  break;  case R.id.add:  break; } }}

下面給出最開始的布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/scan" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="掃一掃"  android:textSize="16sp"/> </LinearLayout> <View android:layout_width="wrap_content" android:layout_height="0.5dp" android:background="#cdcdcd"/> <LinearLayout android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="加好友"  android:textSize="16sp"/> </LinearLayout></LinearLayout>

在activity中調用自定義彈出菜單看看目前的效果

Android,PopWindow,QQ,微信

調用的代碼MyPopWindow win = new MyPopWindow (MainActivity.this, 200,150);指定了彈出菜單的寬高,如果不給就會默認給出wrap_content,就會沾滿整個屏幕的寬度。這個樣子還是比較簡陋,現在在布局文件中加上.9圖的背景圖,在來看看效果

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/title_function_bg"> <LinearLayout android:id="@+id/scan" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="掃一掃"  android:textSize="16sp"/> </LinearLayout> <View android:layout_width="wrap_content" android:layout_height="0.5dp" android:background="#cdcdcd"/> <LinearLayout android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="加好友"  android:textSize="16sp"/> </LinearLayout></LinearLayout>

運行后的效果

Android,PopWindow,QQ,微信

美觀了一點,但是背景后面還有背景什么情況,那么問題來了,怎么解決這個問題?那就需要在popwin的構造方法中加入setBackgroundDrawable(new BitmapDrawable()),難看的方形背景就會消失了。

Android,PopWindow,QQ,微信

接近目標效果了,現在的問題是,每次增加一個菜單項都要手動的定制寬高很煩人,想讓它自己適應高度、寬度,所以那就得修改布局文件了,想想android能夠自由增加item的控件不少,首先想到的就是listview。修改布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/title_function_bg"> <ListView android:id="@+id/title_list" android:layout_width="120dp" android:layout_height="fill_parent" android:cacheColorHint="#00000000" android:divider="@drawable/popu_line" android:padding="3dp" android:scrollingCache="false" android:listSelector="@drawable/title_list_selector"/></LinearLayout>

然后修改自定義的popwindow

public class CustomWin extends PopupWindow { private Context context; private View view; private ListView listView; private List<String> list; public CustomWin(Context context) { this(context, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); } public CustomWin(Context context, int with, int height) { this.context = context; setWidth(with); setHeight(height); //設置可以獲得焦點 setFocusable(true); //設置彈窗內可點擊 setTouchable(true); //設置彈窗外可點擊 setOutsideTouchable(true); setBackgroundDrawable(new BitmapDrawable()); view = LayoutInflater.from(context).inflate(R.layout.popwin_menu,null); setContentView(view); setAnimationStyle(R.style.popwin_anim_style); initData(); } private void initData() { listView = (ListView) view.findViewById(R.id.title_list); list = new ArrayList<String>(); list.add("添加好友"); list.add("掃一掃"); list.add("支付寶"); list.add("視頻聊天"); //設置列表的適配器 listView.setAdapter(new BaseAdapter() { @Override public View getView(int position, View convertView, ViewGroup parent) { TextView textView = null; if (convertView == null) {  textView = new TextView(context);  textView.setTextColor(Color.rgb(255,255,255));  textView.setTextSize(14);  //設置文本居中  textView.setGravity(Gravity.CENTER);  //設置文本域的范圍  textView.setPadding(0, 13, 0, 13);  //設置文本在一行內顯示(不換行)  textView.setSingleLine(true); } else {  textView = (TextView) convertView; } //設置文本文字 textView.setText(list.get(position)); //設置文字與圖標的間隔 textView.setCompoundDrawablePadding(0); //設置在文字的左邊放一個圖標 textView.setCompoundDrawablesWithIntrinsicBounds(R.mipmap., null, null, null); return textView; } @Override public long getItemId(int position) { return position; } @Override public Object getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } }); }}

最終效果:

Android,PopWindow,QQ,微信

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 正定县| 庄河市| 攀枝花市| 涟水县| 塔城市| 涿鹿县| 丹东市| 广州市| 梅州市| 昌吉市| 重庆市| 乌海市| 徐州市| 天津市| 昭通市| 县级市| 龙井市| 囊谦县| 平罗县| 涟水县| 夏邑县| 银川市| 怀柔区| 突泉县| 响水县| 阿拉善左旗| 隆林| 肇源县| 阿巴嘎旗| 天峨县| 东兴市| 南漳县| 始兴县| 天津市| 平阴县| 乐都县| 酉阳| 称多县| 南康市| 武胜县| 岗巴县|