二维码 关注公众号 送价值699元超级大礼包
登陆

国内优质的
素材站点

当前位置: 首页  >  脚本代码  >  android中用PopUpWindow自定义Menu半透明菜单

android中用PopUpWindow自定义Menu半透明菜单

版权信息:站内会员所分享的全部“资源/素材”,仅供学习与参考,版权为原作者所有。
绘艺素材素材站-推荐您购买100%正版素材

android中用PopUpWindow自定义Menu半透明菜单
格 式: JS
更新时间:2014-12-08 15-10
素材模式:免费
描述:
android中用PopUpWindow自定义Menu半透明菜单 
<span style="font-size:18px;">import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;
 
public class MenuTest extends Activity implements OnItemClickListener{
 
private int[] resArray = new int[] {
R.drawable.icon_menu_addto, R.drawable.icon_menu_audioinfo,
R.drawable.icon_menu_findlrc, R.drawable.icon_menu_scan
};
private String[] title = new String[]{
"添加歌曲", "歌曲信息", "查找歌词", "搜索歌词"
};
private PopupWindow pw = null;
private LayoutInflater inflater = null;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initPopupWindow();
    }
    
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        menu.add("menu");// 必须创建一项  
        return super.onCreateOptionsMenu(menu);  
    }
    
    /** 
     * 拦截MENU 
     */  
    @Override  
    public boolean onMenuOpened(int featureId, Menu menu) {  
        if(pw != null){  
            if(!pw.isShowing()){  
                /*最重要的一步:弹出显示   在指定的位置(parent)  最后两个参数 是相对于 x / y 轴的坐标*/  
            pw.showAtLocation(findViewById(R.id.tv), Gravity.CENTER, 0, 300);  
            }  
        }  
        return false;// 返回为true 则显示系统menu  
    }  
    
    public void initPopupWindow(){
    inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);      
View view = inflater.inflate(R.layout.menu, null);
GridView grid1 = (GridView)view.findViewById(R.id.menuGridChange);
grid1.setOnItemClickListener(this);
grid1.setFocusableInTouchMode(true);
grid1.setAdapter(new ImageAdapter(this));
//用Popupwindow弹出menu
pw = new PopupWindow(view,LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
pw.setFocusable(true);
pw.setBackgroundDrawable(new ColorDrawable());
grid1.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_MENU)&&(pw.isShowing())) { 
           pw.dismiss();
           return true;  
       }
return false;
}
});
    }
    
public class ImageAdapter extends BaseAdapter {
 
private Context context;
 
public ImageAdapter(Context context) {
this.context = context;
}
 
@Override
public int getCount() {
return resArray.length;
}
 
@Override
public Object getItem(int arg0) {
return resArray[arg0];
}
 
@Override
public long getItemId(int arg0) {
return arg0;
}
 
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
LinearLayout linear = new LinearLayout(context);
LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
linear.setOrientation(LinearLayout.VERTICAL);
//装进一张图片
ImageView iv = new ImageView(context);
iv.setImageBitmap(((BitmapDrawable)context.getResources().getDrawable(resArray[arg0])).getBitmap());
LinearLayout.LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params2.gravity=Gravity.CENTER;
linear.addView(iv, params2);
//显示文字
TextView tv = new TextView(context);
tv.setText(title[arg0]);
LinearLayout.LayoutParams params3 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params3.gravity=Gravity.CENTER;
 
linear.addView(tv, params3);
 
return linear;
}
}
 
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
switch (position) {
case 0:
pw.dismiss();
final String[] items = {"java",".net","php"};   
       new AlertDialog.Builder(this)   
           .setTitle("选择语言")                   //此处  this  代表当前Activity   
           .setItems(items, new DialogInterface.OnClickListener() {   
                public void onClick(DialogInterface dialog, int item) {   
                switch (item) {
case 0:
 
Toast.makeText(MenuTest.this, items[item], Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(MenuTest.this, items[item], Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(MenuTest.this, items[item], Toast.LENGTH_LONG).show();
break;
                }
                 }   
       }).show();//显示对话框
break;
case 1:
Toast.makeText(this, "第二个", Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(this, "第三个", Toast.LENGTH_LONG).show();
break;
case 3:
Toast.makeText(this, "第四个", Toast.LENGTH_LONG).show();
break;
}
}
 
}</span>
下面是mian.xml文件:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
android:id="@+id/tv"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
</LinearLayout></span>
接着是menu.xml文件:
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<GridView android:gravity="center"
android:layout_gravity="center"
android:id="@+id/menuGridChange"
android:background="@drawable/menu_bg_frame"
android:layout_alignParentBottom="true"
android:padding="5.0dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="10.0dip"
android:verticalSpacing="3.0dip"
android:stretchMode="columnWidth"
android:columnWidth="60.0dip"
android:numColumns="auto_fit"
xmlns:android="http://schemas.android.com/apk/res/android" />
</RelativeLayout></span>
下面是自定义的PopupWindow样式的xml文件,menu_bg_frame.xml:
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<!-- 半透明圆角背景 -->
<shape android:shape="rectangle"
  xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#b4000000" />
    <stroke android:width="2.0dip" android:color="#b4ffffff" android:dashWidth="3.0dip" android:dashGap="0.0dip" />
    <padding android:left="7.0dip" android:top="7.0dip" android:right="7.0dip" android:bottom="7.0dip" />
    <corners android:radius="8.0dip" />
</shape></span>

本周最热素材推荐
  • 基于JQuery新浪新版带箭头的图片左右滚动代码
  • jQuery横向带箭头左右滚动效果
  • jquery左右循环滚动图片代码
  • 成长记录线公司大事记jQuery时间轴_时间日期_JS代码
  • JS动态可控制左右滚动的图片
  • css3-rotate实现超炫环形旋转特效
 绘艺素材素材站所有作品部分为用户自行上传分享并拥有版权或使用权,全站素材仅供网友学习交流,版权归原作者所有。
相关搜索标签
反馈