天天动听 半透明Menu效果

本文介绍了一种在Android应用中创建半透明圆角背景菜单的方法,包括使用Shape定义背景样式、GridView布局设置以及通过PopupWindow展示菜单的具体实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先看效果图:

\
 

分解一下:
1. 利用Shaper设置一个半透明圆角背景
2. 定义Menu布局,主要就GridView,把图标都放在这个GridView
3. Menu事件, 通过PopupWindow或者AlertDialog或者透明Activity显示到页面即可。
4. 按钮的监听事件,实例中没加。需要的话自己在Adapter里加
半透明圆角背景:

1 <?xml version="1.0" encoding="UTF-8"?>
2 <shape android:shape="rectangle"
3 xmlns:android="http://schemas.android.com/apk/res/android">
4 <solid android:color="#b4000000" />
5 <stroke android:width="2.0dip" android:color="#b4ffffff"android:dashWidth="3.0dip" android:dashGap="0.0dip" />
6 <padding android:left="7.0dip" android:top="7.0dip"android:right="7.0dip" android:bottom="7.0dip" />
7 <corners android:radius="8.0dip" />
8 </shape>
Menu布局:
01 <?xml version="1.0" encoding="UTF-8"?>
02  
03 <LinearLayout
04  
05 android:orientation="vertical"
06  
07 android:layout_width="wrap_content"
08  
09 android:layout_height="fill_parent"
10  
11 xmlns:android="http://schemas.android.com/apk/res/android">
12  
13 <GridView android:gravity="center"
14  
15 android:layout_gravity="center"
16  
17 android:id="@+id/menuGridChange"
18  
19 android:background="@drawable/menu_bg_frame"
20  
21 android:padding="5.0dip"
22  
23 android:layout_width="fill_parent"
24  
25 android:layout_height="wrap_content"
26  
27 android:horizontalSpacing="10.0dip"
28  
29 android:verticalSpacing="3.0dip"
30  
31 android:stretchMode="columnWidth"
32  
33 android:columnWidth="60.0dip"
34  
35 android:numColumns="auto_fit"
36  
37 xmlns:android="http://schemas.android.com/apk/res/android" />
38  
39 </LinearLayout>
主要类:
001 package com.yfz;
002 import android.app.Activity;
003 import android.app.AlertDialog;
004 import android.app.AlertDialog.Builder;
005 import android.content.Context;
006 import android.graphics.drawable.BitmapDrawable;
007 import android.os.Bundle;
008 import android.util.Log;
009 import android.view.ContextMenu;
010 import android.view.Gravity;
011 import android.view.LayoutInflater;
012 import android.view.Menu;
013 import android.view.MenuItem;
014 import android.view.View;
015 import android.view.ViewGroup;
016 import android.view.ContextMenu.ContextMenuInfo;
017 import android.widget.BaseAdapter;
018 import android.widget.GridView;
019 import android.widget.ImageView;
020 import android.widget.LinearLayout;
021 import android.widget.PopupWindow;
022 import android.widget.TextView;
023 import android.widget.LinearLayout.LayoutParams;
024 public class MenuTest extends Activity {
025 private String TAG = this.getClass().getSimpleName();
026 private int[] resArray = new int[] {
027 R.drawable.icon_menu_addto, R.drawable.icon_menu_audioinfo,
028 R.drawable.icon_menu_findlrc, R.drawable.icon_menu_scan
029 };
030 private String[] title = new String[]{
031 "添加歌曲""歌曲信息""查找歌词""搜索歌词"
032 };
033 private static boolean show_flag = false;
034 private PopupWindow pw = null;
035 /** Called when the activity is first created. */
036 @Override
037 public void onCreate(Bundle savedInstanceState) {
038 super.onCreate(savedInstanceState);
039 setContentView(R.layout.main);
040 }
041 @Override
042 public boolean onCreateOptionsMenu(Menu menu) {
043 Log.e(TAG, "------ onCreateOptionsMenu ------");
044 //用AlertDialog弹出menu
045 // View view = LayoutInflater.from(this).inflate(R.layout.menu, null);
046 // GridView grid1 = (GridView)view.findViewById(R.id.menuGridChange);
047 // grid1.setAdapter(new ImageAdapter(this));
048 // Builder build = new AlertDialog.Builder(this);
049 // build.setView(view);
050 // build.show();
051 LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
052 View view = inflater.inflate(R.layout.menu, null);
053 GridView grid1 = (GridView)view.findViewById(R.id.menuGridChange);
054 grid1.setAdapter(new ImageAdapter(this));
055 //用Popupwindow弹出menu
056 pw = new PopupWindow(view,LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
057 //NND, 第一个参数, 必须找个View
058 pw.showAtLocation(findViewById(R.id.tv), Gravity.CENTER, 0300);
059 return true;
060 }
061 @Override
062 public boolean onOptionsItemSelected(MenuItem item) {
063 return super.onOptionsItemSelected(item);
064 }
065 public class ImageAdapter extends BaseAdapter {
066 private Context context;
067 public ImageAdapter(Context context) {
068 this.context = context;
069 }
070 @Override
071 public int getCount() {
072 return resArray.length;
073 }
074 @Override
075 public Object getItem(int arg0) {
076 return resArray[arg0];
077 }
078 @Override
079 public long getItemId(int arg0) {
080 return arg0;
081 }
082 @Override
083 public View getView(int arg0, View arg1, ViewGroup arg2) {
084 LinearLayout linear = new LinearLayout(context);
085 LinearLayout.LayoutParams params = newLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
086 linear.setOrientation(LinearLayout.VERTICAL);
087 ImageView iv = new ImageView(context);
088 iv.setImageBitmap(((BitmapDrawable)context.getResources().getDrawable(resArray[arg0])).getBitmap());
089 LinearLayout.LayoutParams params2 = newLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
090 params2.gravity=Gravity.CENTER;
091 linear.addView(iv, params2);
092 TextView tv = new TextView(context);
093 tv.setText(title[arg0]);
094 LinearLayout.LayoutParams params3 = newLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
095 params3.gravity=Gravity.CENTER;
096 linear.addView(tv, params3);
097 return linear;
098 }
099 }
100 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值