2011-12-06 10:05:35|  分类: android |  标签:android  popwindow  |字号 订阅

恩恩,记录下最近弄的一个新的功能,这个功能就是自己定义一个spinner弹出下拉框的样式。 之前的想法是能否通过修改spinner的弹出下拉框窗口而修改样式,在google上得出的结果是使用popwindow。 popwindow就是一个自定义弹出窗口 ,具体解释是: A popup window that can be used to display an arbitrary view. The popup windows is a floating container that appears on top of the current activity. 一个弹出窗口,可以用来显示一个任意视图。弹出窗口是当前活动的顶部出现一个浮动的容器。
我立即想到可以通过点击按钮触发弹出一个popwindow,这样来达到显示自定义的spinner下拉弹出窗口的效果。 具体实现代码如下:
     

package com.test.ui; import android.app.Activity; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.Gravity; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.PopupWindow; import android.widget.Toast; public class TestActivity extends Activity { /** Called when the activity is first created. */ private Button but_menu; private Button open_id; private Button save_id; View contentView; private PopupWindow m_popupWindow; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); setListener(); } private void init() { contentView = getLayoutInflater().inflate(R.layout.popupmenu, null, true); but_menu = (Button) findViewById(R.id.but_menu); open_id = (Button) contentView.findViewById(R.id.btn_popup_information); save_id = (Button) contentView.findViewById(R.id.btn_popup_quote);

// PopupWindow弹出的窗口显示的view,第二和第三参数:分别表示此弹出窗口的大小 m_popupWindow = new PopupWindow(contentView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true); m_popupWindow.setBackgroundDrawable(new BitmapDrawable());//有了这句才可以点击返回(撤销)按钮dismiss()popwindow m_popupWindow.setOutsideTouchable(true); m_popupWindow.setAnimationStyle(R.style.PopupAnimation); } private void setListener() { contentView.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub m_popupWindow.dismiss(); } }); // m_popupWindow = new PopupWindow(); but_menu.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { if (m_popupWindow.isShowing()) { m_popupWindow.dismiss(); } m_popupWindow.showAsDropDown(v); } catch (Exception e) { Toast.makeText(TestActivity.this, e.getMessage(), Toast.LENGTH_SHORT); } } }); open_id.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { m_popupWindow.dismiss(); Toast.makeText(TestActivity.this, "打开被触发", Toast.LENGTH_SHORT) .show(); } }); save_id.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { m_popupWindow.dismiss(); Toast.makeText(TestActivity.this, "保存被触发", Toast.LENGTH_SHORT) .show(); } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (m_popupWindow != null && m_popupWindow.isShowing()) { m_popupWindow.dismiss(); return true; } } return super.onKeyDown(keyCode, event); } }

popupmenu.xml
     

<?xml version="1.0" encoding="utf-8"?> android:id="@+id/lin_main" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" android:paddingRight="120dip"> android:background="@drawable/popup_search" android:orientation="vertical" android:layout_height="wrap_content" android:layout_width="wrap_content"> android:layout_width="fill_parent" android:background="@drawable/goods_bg" android:id="@+id/btn_popup_information" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="16sp" android:text="打开" android:layout_weight="1" /> android:id="@+id/btn_popup_quote" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="保存" android:background="@drawable/goods_bg" android:layout_weight="1" android:textColor="#000000" android:textSize="16sp" android:layout_marginTop="5dip" /> android:id="@+id/btn_popup_product" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="关于我们" android:background="@drawable/goods_bg" android:layout_weight="1" android:textColor="#000000" android:textSize="16sp" android:layout_marginTop="5dip" /> android:id="@+id/btn_popup_buy" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="关闭" android:background="@drawable/shop_bg" android:layout_weight="1" android:textColor="#000000" android:textSize="16sp" android:layout_marginTop="5dip" />

下面的是popwindow出现和退出的动画特效,这种东西网上有很多,就拿出这个吧. values/style.xml
     

<?xml version="1.0" encoding="utf-8"?> name="PopupAnimation" parent="android:Animation"> name="android:windowEnterAnimation">@anim/popup_search_show name="android:windowExitAnimation">@anim/popup_search_hide

anim/popup_search_show.xml
     

<?xml version="1.0" encoding="utf-8"?> xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator" > android:duration="500" android:pivotX="0.100000024%" android:pivotY="0.0" android:fromXScale="1.0" android:toXScale="0.0" android:fromYScale="1.0" android:toYScale="0.0" >

anim/popup_search_hide.xml
     

<?xml version="1.0" encoding="utf-8"?> xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator" > android:duration="500" android:pivotX="0.100000024%" android:pivotY="0.0" android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.0" android:toYScale="1.0" >

在popupmenu.xml里用到了两个 LinearLayout,是用来控制popwindow的显示大小。
效果图: 现在唯一做得不好的就是背景没有变灰,虽然我有办法将背景变灰,但是效果都不满意都没有类似spinner点击后背景变灰的那样漂亮,所以如果有哪位大哥大姐知道怎么弄的话不啬指教,一同分享快乐,一同进步。

更多相关文章

  1. Android中使用ListView绘制自定义表格(2)
  2. Android(安卓)开发篇一(安装环境搭建)
  3. Android(安卓)ProgressBar 自定义样式(一)
  4. Android之富有动感的底部弹窗效果
  5. 【Android】各式各样的弹出框与对菜单键、返回键的监听
  6. android ImageButton示例
  7. Android(安卓)Fragment用法详解(2)--动态添加Fragment
  8. android java开发 第二天 之熟悉 程序开发流程
  9. Android(安卓)Studio Intent使用(显式、隐式) 第一行代码

随机推荐

  1. 调用android 中的拨号功能、调用某个联系
  2. Android属性之android:priority
  3. Android笔记--handler机制
  4. Android XML文件解析
  5. Android(安卓)基本原理&移动终端软件开发
  6. Android中 LED 系统架构
  7. 原YUV格式的解析 Android(安卓)NV21 视频
  8. [Android开发]android 跨进程通信之conte
  9. android的图片加载和伸缩处理
  10. 检测Android本地存储是否有敏感信息