使用popupwindow设计出popupmenu效果的菜单

1、popupwindow的布局文件:menu_layout.xml

     <?xml version="1.0" encoding="utf-8"?>
     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:background="@drawable/rectangle"
        android:divider="#d8d8d8"
        android:dividerHeight="1px" >
    </ListView>

</RelativeLayout>


2、菜单项的布局文件:menu_item.xml

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content" 
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
android:layout_centerInParent="true" 
android:gravity="center"       
        android:textColor="#FFFFFF"
        android:textSize="10.5sp" 
        android:text="dsss"/>
  
</RelativeLayout>


3、popupwindow的背景设置:rectangle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
<!-- 定义使用渐变色填充 -->
    <!-- <gradient
        android:angle="270"
        android:endColor="#000000"
        android:startColor="#000000" /> -->

    <!-- 定义几何形状的内边距 -->
    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp" />

    <!-- 定义几何形状的大小 -->
    <size
        android:height="60dp"
        android:width="30dp" />
    
    <!-- 定义使用单种颜色填充 -->
    <solid 
        android:color="#000000"/>
    
    <!-- 绘制边框 -->
    <stroke 
        android:width="1px"
        android:color="#f4b00f"/>
</shape>


4、Activity

package com.example.httptest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;

public class PopupWindowActivity extends Activity {
private PopupWindow popupWindow;
private View parent;

/** 菜单弹出来时候的菜单项文字 */
private String[] names = { "编辑相册", "删除相册","删除相片" };

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bn = (Button) findViewById(R.id.bn);
/** PopupWindow的界面 */
View contentView = getLayoutInflater().inflate(R.layout.menu_layout, null);
/** 列表布局界面 */
ListView listView = (ListView) contentView.findViewById(R.id.listView);
/** 设置网格布局的适配器 */
listView.setAdapter(getAdapter());
/** 设置网格布局的菜单项点击时候的Listener */
listView.setOnItemClickListener(new ItemClickListener());
/** 初始化PopupWindow */
popupWindow = new PopupWindow(contentView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//popupWindow.
popupWindow.setFocusable(true);// 取得焦点
popupWindow.setBackgroundDrawable(new BitmapDrawable());
/** 设置PopupWindow弹出和退出时候的动画效果 */
//popupWindow.setAnimationStyle(R.style.animation);
parent = this.findViewById(R.id.main);
}

private final class ItemClickListener implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (popupWindow.isShowing()) {
popupWindow.dismiss();// 关闭
}
}
}


/** 返回网格布局的适配器 */
private ListAdapter getAdapter() {
List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < names.length; i++) {
HashMap<String, Object> item = new HashMap<String, Object>();
item.put("name", names[i]);
data.add(item);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, data,
R.layout.menu_item, new String[] { "name" },
new int[] { R.id.textView });
return simpleAdapter;
}


public void openPopWindow(View v) {
/** 设置PopupWindow弹出后的位置 */
//popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
int[] location = new int[2];  
        v.getLocationOnScreen(location);  
        Log.v("li","按钮的位置:"+location[0]+","+location[1]);
        //popupWindow.showAsDropDown(v);
        int w = location[0]-10;
        //popupWindow.getHeight()
        int h = location[1]+70;
        Log.v("li","popupwindow的位置:"+w+","+h);
        popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0] + 25, location[1] + 67);

}
}


5、效果图


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力回答你的问题。 要实现类似微信聊天消息长按弹菜单效果,可以使用 PopupWindow 组件来实现。下面是实现的大致步骤: 1. 在布局文件中定义一个包含要弹菜单项的布局文件,例如一个 LinearLayout。 2. 在代码中创建 PopupWindow 对象,并设置其内容为上一步中定义的布局文件。 3. 设置 PopupWindow 的大小、位置、背景等属性。 4. 监听长按事件,在长按事件中弹 PopupWindow。 5. 监听 PopupWindow菜单项的点击事件,处理相应的逻辑。 具体实现细节可以参考以下代码: ``` // 定义 PopupWindow PopupWindow popupWindow = new PopupWindow(context); View contentView = LayoutInflater.from(context).inflate(R.layout.menu_layout, null); popupWindow.setContentView(contentView); popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 监听长按事件 view.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { // 显示 PopupWindow int[] location = new int[2]; v.getLocationOnScreen(location); popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1] - popupWindow.getHeight()); return true; } }); // 监听菜单项的点击事件 TextView menuItem = contentView.findViewById(R.id.menu_item); menuItem.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理菜单项的逻辑 popupWindow.dismiss(); } }); ``` 希望这个回答能够帮到你,如果还有其他问题,可以继续问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值