为什么我的手机android 下拉弹窗窗时有时无

全部微信分类
当前位置:
本网站文章均来自网络
搜索结果,只以信息传播为目的,不代表本站认同其观点和立场,版权为原作者所有,请与公众号原作者联系,亦切勿轻易相信文章内容。如有侵犯权益,或者不良信息。请立即联络我们做技术屏蔽! 邮箱:
本网站文章均来自网络
搜索结果,只以信息传播为目的,不代表本站认同其观点和立场,版权为原作者所有,请与公众号原作者联系,亦切勿轻易相信文章内容。如有侵犯权益,或者不良信息。请立即联络我们做技术屏蔽! 邮箱:
上一篇:第一篇
下一篇:最后一篇
关注排行榜
友情链接:android利用PopupWindow实现点击工具栏弹出下拉菜单
本文将介绍如何利用PopupWindow实现点击屏幕顶部工具栏按钮弹出下拉菜单的功能。先上图:
2.代码实现
首先是activity_main.xml布局文件:
其中,rl_topbar定义了顶部工具栏,里面包含左中右三个TextView,点击这三个控件会弹出相应的下拉菜单。
接下来是MainActivity.java文件:
package com.example.
import java.util.ArrayL
import java.util.HashM
import java.util.L
import java.util.M
import android.os.B
import android.app.A
import android.graphics.drawable.ColorD
import android.view.M
import android.view.MotionE
import android.view.V
import android.view.ViewGroup.LayoutP
import android.widget.AdapterV
import android.widget.ListV
import android.widget.PopupW
import android.widget.RelativeL
import android.widget.SimpleA
import android.widget.TextV
public class MainActivity extends Activity {
private RelativeLayout rlTopB
// 左中右三个控件(工具栏里)
private TextView tvL
private TextView tvR
private TextView tvM
// 左中右三个弹出窗口
private PopupWindow popL
private PopupWindow popR
private PopupWindow popM
// 左中右三个layout
private View layoutL
private View layoutR
private View layoutM
// 左中右三个ListView控件(弹出窗口里)
private ListView menulistL
private ListView menulistR
private ListView menulistM
// 菜单数据项
private List private List private List protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initParam();
private void initParam() {
rlTopBar = (RelativeLayout) this.findViewById(R.id.rl_topbar);
tvLeft = (TextView) this.findViewById(R.id.tv_left);
tvLeft.setOnClickListener(myListener);
// 初始化数据项
listLeft = new ArrayList for (int i = 1; i & 10; i++) {
HashMap mapTemp = new HashMap();
mapTemp.put(item, left
listLeft.add(mapTemp);
tvRight = (TextView) this.findViewById(R.id.tv_right);
tvRight.setOnClickListener(myListener);
// 初始化数据项
listRight = new ArrayList for (int i = 1; i & 10; i++) {
HashMap mapTemp = new HashMap();
mapTemp.put(item, right
listRight.add(mapTemp);
tvMiddle = (TextView) this.findViewById(R.id.tv_middle);
tvMiddle.setOnClickListener(myListener);
// 初始化数据项
listMiddle = new ArrayList for (int i = 1; i & 10; i++) {
HashMap mapTemp = new HashMap();
mapTemp.put(item, mid
listMiddle.add(mapTemp);
private View.OnClickListener myListener = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_left:
if (popLeft != null && popLeft.isShowing()) {
popLeft.dismiss();
layoutLeft = getLayoutInflater().inflate(
R.layout.pop_menulist, null);
menulistLeft = (ListView) layoutLeft
.findViewById(R.id.menulist);
SimpleAdapter listAdapter = new SimpleAdapter(
MainActivity.this, listLeft, R.layout.pop_menuitem,
new String[] { item },
new int[] { R.id.menuitem });
menulistLeft.setAdapter(listAdapter);
// 点击listview中item的处理
menulistLeft
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView arg0,
View arg1, int arg2, long arg3) {
// 改变顶部对应TextView值
String strItem = listLeft.get(arg2).get(
tvLeft.setText(strItem);
// 隐藏弹出窗口
if (popLeft != null && popLeft.isShowing()) {
popLeft.dismiss();
// 创建弹出窗口
// 窗口内容为layoutLeft,里面包含一个ListView
// 窗口宽度跟tvLeft一样
popLeft = new PopupWindow(layoutLeft, tvLeft.getWidth(),
LayoutParams.WRAP_CONTENT);
ColorDrawable cd = new ColorDrawable(-0000);
popLeft.setBackgroundDrawable(cd);
popLeft.setAnimationStyle(R.style.PopupAnimation);
popLeft.update();
popLeft.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
popLeft.setTouchable(true); // 设置popupwindow可点击
popLeft.setOutsideTouchable(true); // 设置popupwindow外部可点击
popLeft.setFocusable(true); // 获取焦点
// 设置popupwindow的位置(相对tvLeft的位置)
int topBarHeight = rlTopBar.getBottom();
popLeft.showAsDropDown(tvLeft, 0,
(topBarHeight - tvLeft.getHeight()) / 2);
popLeft.setTouchInterceptor(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// 如果点击了popupwindow的外部,popupwindow也会消失
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
popLeft.dismiss();
case R.id.tv_right:
if (popRight != null && popRight.isShowing()) {
popRight.dismiss();
layoutRight = getLayoutInflater().inflate(
R.layout.pop_menulist, null);
menulistRight = (ListView) layoutRight
.findViewById(R.id.menulist);
SimpleAdapter listAdapter = new SimpleAdapter(
MainActivity.this, listRight, R.layout.pop_menuitem,
new String[] { item },
new int[] { R.id.menuitem });
menulistRight.setAdapter(listAdapter);
// 点击listview中item的处理
menulistRight
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView arg0,
View arg1, int arg2, long arg3) {
String strItem = listRight.get(arg2).get(
tvRight.setText(strItem);
if (popRight != null && popRight.isShowing()) {
popRight.dismiss();
popRight = new PopupWindow(layoutRight, tvRight.getWidth(),
LayoutParams.WRAP_CONTENT);
ColorDrawable cd = new ColorDrawable(-0000);
popRight.setBackgroundDrawable(cd);
popRight.setAnimationStyle(R.style.PopupAnimation);
popRight.update();
popRight.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
popRight.setTouchable(true); // 设置popupwindow可点击
popRight.setOutsideTouchable(true); // 设置popupwindow外部可点击
popRight.setFocusable(true); // 获取焦点
// 设置popupwindow的位置
int topBarHeight = rlTopBar.getBottom();
popRight.showAsDropDown(tvRight, 0,
(topBarHeight - tvRight.getHeight()) / 2);
popRight.setTouchInterceptor(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// 如果点击了popupwindow的外部,popupwindow也会消失
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
popRight.dismiss();
case R.id.tv_middle:
if (popMiddle != null && popMiddle.isShowing()) {
popMiddle.dismiss();
layoutMiddle = getLayoutInflater().inflate(
R.layout.pop_menulist, null);
menulistMiddle = (ListView) layoutMiddle
.findViewById(R.id.menulist);
SimpleAdapter listAdapter = new SimpleAdapter(
MainActivity.this, listMiddle, R.layout.pop_menuitem,
new String[] { item },
new int[] { R.id.menuitem });
menulistMiddle.setAdapter(listAdapter);
// 点击listview中item的处理
menulistMiddle
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView arg0,
View arg1, int arg2, long arg3) {
String strItem = listMiddle.get(arg2).get(
tvMiddle.setText(strItem);
if (popMiddle != null && popMiddle.isShowing()) {
popMiddle.dismiss();
popMiddle = new PopupWindow(layoutMiddle, tvMiddle.getWidth(),
LayoutParams.WRAP_CONTENT);
ColorDrawable cd = new ColorDrawable(-0000);
popMiddle.setBackgroundDrawable(cd);
popMiddle.setAnimationStyle(R.style.PopupAnimation);
popMiddle.update();
popMiddle.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
popMiddle.setTouchable(true); // 设置popupwindow可点击
popMiddle.setOutsideTouchable(true); // 设置popupwindow外部可点击
popMiddle.setFocusable(true); // 获取焦点
// 设置popupwindow的位置
int topBarHeight = rlTopBar.getBottom();
popMiddle.showAsDropDown(tvMiddle, 0,
(topBarHeight - tvMiddle.getHeight()) / 2);
popMiddle.setTouchInterceptor(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// 如果点击了popupwindow的外部,popupwindow也会消失
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
popMiddle.dismiss();
public boolean onCreateOptionsMenu(Menu menu) {
// I this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
以上代码有详细的注释,这里就不做过多解释啦!原理比较简单,点击工具栏上的TextView会弹出PopupWindow,里面包含ListView,显示所有菜单项。
弹出的菜单窗口(PopupWindow)布局文件为pop_menulist.xml,就一个ListView
而每个菜单项对应的布局文件为pop_menuitem.xml,只包含一个TextView
其他还有一些属性资源相关的文件,不是很重要,大家可以下载工程进行研究!
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'

参考资料

 

随机推荐