點選主介面 PopupMenu消失
我們有時想做出彈出PopupMenu,主介面變暗,點選主介面,PopupMenu消失。主程式碼如下
package com.example.mypopupwindow;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListPopupWindow;
@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnClickListener, OnDismiss {
private List<String> titles;
private List<List<String>> item_names;
private List<List<Integer>> item_images;
private ListPopupWindow myPopupMenu;
ImageView bg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titles = new ArrayList<String>();
titles = addItems(new String[]{"menu"});
item_images = new ArrayList<List<Integer>>();
item_images.add(addItems(new Integer[] { R.drawable.ic_action_call,
R.drawable.ic_action_camera, R.drawable.ic_action_copy,
R.drawable.ic_action_crop}));
item_names = new ArrayList<List<String>>();
item_names.add(addItems(new String[] { "aaaaaaaaa", "bbbbbbbb", "ccccccc", "ddddddd", "eeeeeeee",
"fffff", "ggggggg", "hhhhhhh" }));
myPopupMenu = new ListPopupWindow(this);
Resources res = this.getResources();
myPopupMenu.setBackgroundDrawable(res.getDrawable(R.drawable.menu_bg));
myPopupMenu.setWidth(600);
myPopupMenu.setHeight(LayoutParams.WRAP_CONTENT);
BodyAdapter bodyAdapter = new BodyAdapter(this, item_names.get(0),item_images.get(0));
myPopupMenu.setAdapter(bodyAdapter);
bg = (ImageView)findViewById(R.id.overlay);
bg.setOnClickListener(this);
ActionBar acb = getActionBar();
acb.hide();
}
private List<String> addItems(String[] values) {
List<String> list = new ArrayList<String>();
for (String var : values) {
list.add(var);
}
return list;
}
private List<Integer> addItems(Integer[] values) {
List<Integer> list = new ArrayList<Integer>();
for (Integer var : values) {
list.add(var);
}
return list;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("menu");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (myPopupMenu.isShowing()) {
myPopupMenu.dismiss();
} else {
myPopupMenu.show();
}
return false;
}
public void showpopupwindow(View view){
if (myPopupMenu.isShowing()) {
myPopupMenu.dismiss();
} else {
myPopupMenu.setAnchorView(view);
myPopupMenu.show();
bg.setVisibility(View.VISIBLE);
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int id = arg0.getId();
if(id == R.id.overlay){
bg.setVisibility(View.GONE);
}
}
@Override
public void onDismiss() {
// TODO Auto-generated method stub
bg.setVisibility(View.GONE);
}
}
package com.example.mypopupwindow;
public interface OnDismiss {
public void onDismiss();
}
package com.example.mypopupwindow;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class BodyAdapter extends BaseAdapter{
private List<String> item_names;
private List<Integer> item_images;
private Context mContext;
public BodyAdapter(Context context,List<String> item_names,List<Integer> item_images){
this.mContext = context;
this.item_names = item_names;
this.item_images = item_images;
}
@Override
public int getCount() {
return item_images.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(mContext);
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.pada_browser_menu_item, parent,
false);
}
TextView title = (TextView) view.findViewById(R.id.item_title);
title.setText(item_names.get(position));
return view;
}
}
然後再定義兩個xml檔案
pada_browser_menu_item.xml
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="100px"
android:textSize="22dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="18dp"
android:gravity="center"
/>
</RelativeLayout>
相關文章
- APPIUM+IOS 點選介面元素APPiOS
- Android主介面連續兩次點選物理返回鍵退出應用功能實現Android
- 直播網站原始碼,點選EditText以外的區域,鍵盤隱藏消失網站原始碼
- Select2 滑鼠點選空白處不消失簡單測試和解決方法
- 基於etcd的選主功能實現的主備節點管理
- 使用介面實現RecyclerView中的item點選事件View事件
- Laya頁面過後fgui介面不能點選問題GUI
- win10 全屏後選單鍵鍵消失怎麼辦 win10開始選單消失了怎麼辦Win10
- win10獲取焦點滑鼠消失怎麼解決_win10滑鼠焦點消失怎麼辦Win10
- iOS 點選推送訊息跳轉指定介面 —總結篇iOS
- mac電腦文稿在哪裡?文稿介面消失不見如何找回?Mac
- [提問交流]切換頁面,左側選單消失
- KPN當選NEO主網共識節點,加速去中心化程式中心化
- UI介面微信底部(ViewPager實現Tab,左右滑動+底部點選)UIViewpager
- postman(一):主介面模組解析Postman
- elastic-job-lite 既然去中心化,為何要選舉主節點AST中心化
- 安全屋和主選單
- Kudu主鍵選擇策略
- 點選、拖拉拽,BI系統讓業務掌握資料分析主動權
- 介面測試框架選擇框架
- TypeScript 介面 可選屬性TypeScript
- UI | 點選,開始我們的征途! 分享一組旅行APP介面設計UIAPP
- win10快捷方式點選彈出屬性介面怎麼解決Win10
- 消失的150家主體:買量公司的商業模式走到了盡頭?模式
- 短視訊app原始碼,輸入邀請碼時點選「螢幕」周邊或者「返回鍵」彈框不會消失APP原始碼
- 消失的《行者》
- win10系統下網路介面卡消失不見怎麼辦Win10
- 三種方法解決純css下拉選單滑鼠移入選單內容時選單隱藏消失的問題CSS
- 導航欄點選選中
- ztree 篩選選中節點
- 【RocketMQ】DLedger選主原始碼分析MQ原始碼
- MFC介面庫BCGControlBar v27.0新版亮點:視覺化管理器和主題等GC視覺化
- pyqt5主介面構建利器:QStackedWidgetQT
- 小白裝機電腦主機板選購技巧 電腦主機板怎麼選?
- UGUI_關卡選項介面UGUI
- 如何選擇美顏SDK介面?
- 點選一百萬次
- pyautogui點選失效GUI
- 電商 | 如何突出產品本身特點 分享一組主體提取型屬性介面設計