转自:http://www.eoeandroid.com/viewthread.php?tid=45108&extra=&page=1

activity实现半透明的、淡入的menu—供初学者研究学习虽然android带的menu已经很漂亮,但有的时候也需要我们去自定义menu。开始的时候只是想在android上做一个半透明的menu,但是刚刚接触android不是太久,我不知道从何处入手,也不知道如何修改android自带的menu,于是到网上找,结果找了好久有关半透明的menu,都没有找到,只好静下心来研究文档和相关书籍,最后用activity制作出半透明的menu,而且在menu弹出的时候加入了一个淡入的动画。如下图所示:

 

1、首先创建一个工程,命名为translucentButton

2、创建menu layout

想创建什么样式的menu,layout很重要,在layout中,没有用到什么特别的语法,在基础类书籍当中都能查的到,稍微难一点的就是Button上的selector。源码如下:

                    
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.  
  3.  
  4.  
  5. "http://schemas.android.com/apk/res/android" 
  6.  
  7.     android:orientation="vertical" 
  8.  
  9.     android:layout_width="fill_parent" 
  10.  
  11.     android:layout_height="fill_parent" 
  12.  
  13.     android:gravity="bottom">  
  14.  
  15.         //schemas.android.com/apk/res/android  
  16.  
  17.              android:orientation="horizontal" 
  18.  
  19.              android:layout_width="fill_parent" 
  20.  
  21.              android:layout_height="wrap_content" 
  22.  
  23.              android:gravity="bottom">  
  24.  
  25.              
  26.  
  27.               android:id="@+id/menu1" 
  28.  
  29.               android:layout_width="wrap_content" 
  30.  
  31.               android:layout_height="60dp" 
  32.  
  33.               android:text="@string/menu1" 
  34.  
  35.               android:textColor="@drawable/white" 
  36.  
  37.               android:layout_weight="0.25" 
  38.  
  39.               android:background="@drawable/buttonselector">  
  40.  
  41.               
  42.  
  43.             
  44.  
  45.                   android:id="@+id/menu2" 
  46.  
  47.               android:layout_width="wrap_content" 
  48.  
  49.               android:layout_height="60dp" 
  50.  
  51.               android:text="@string/menu2" 
  52.  
  53.               android:textColor="@drawable/white" 
  54.  
  55.               android:layout_weight="0.25" 
  56.  
  57.               android:background="@drawable/buttonselector">  
  58.  
  59.               
  60.  
  61.             
  62.  
  63.               android:id="@+id/menu3" 
  64.  
  65.               android:layout_width="wrap_content" 
  66.  
  67.               android:layout_height="60dp" 
  68.  
  69.               android:text="@string/menu3" 
  70.  
  71.               android:textColor="@drawable/white" 
  72.  
  73.               android:layout_weight="0.25" 
  74.  
  75.               android:background="@drawable/buttonselector">  
  76.  
  77.               
  78.  
  79.             
  80.  
  81.               android:id="@+id/menu4" 
  82.  
  83.               android:layout_width="wrap_content" 
  84.  
  85.               android:layout_height="60dp" 
  86.  
  87.               android:text="@string/menu4" 
  88.  
  89.               android:textColor="@drawable/white" 
  90.  
  91.               android:layout_weight="0.25" 
  92.  
  93.               android:background="@drawable/buttonselector">  
  94.  
  95.               
  96.  
  97.           
  98.  
  99.  

下载 (281.55 KB)
menu demo
2010-11-18 17:52

3、在menu上定义style,限制menu的区域把不用的区域隐藏起来。Styles.xml如下:

            
  1.  
  2.  
  3.          "Theme.Translucent" parent="android:style/Theme.Translucent">  
  4.  
  5.               "android:windowBackground">@drawable/translucent_background  
  6.  
  7.               "android:windowNoTitle">true  
  8.  
  9.               "android:colorForeground">#fff  
  10.  
  11.            
  12.  
  13.  

4、在AndroidManifest.xml文件activity元素中一定要加上这样一句:android:theme=“@style/Theme.Translucent”。这句话的意思是使用我们自定义的name为Theme.Translucent风格。

 


5、在主activity中显示menu,需要在onKeyUp()中监听menu button按下的消息,当menu button按下后,显示我们自定义的menu(其实是activity),再次按下时关闭menu。而且显示menu时,我加入了一个动画,加入的方法很简单,只需调用overridePendingTransiton(R.anim.fade, R.anim.hold),这个函数的参数是自定义的动画xml。详见附件!


总结

这篇文章主要是用activity制作了一个menu,并实现半透明。使用了style,而且自定义的style是继承自android:style/Theme.Translucent,如果不继承它,在style的name字段中是不能使用点(.)的,大家可自行验证。同时也使用的animation,只是出于学习的目的。最后祝大家学习愉快,学习的过程中可以把自己的成果拿出来跟大家分享。谢谢!

更多相关文章

  1. android 基础学习(6)-----sqlite3查看表结构
  2. J2ME to android之学习笔记
  3. Android(安卓)Animation学习笔记
  4. Android(安卓)Studio学习之安装插件
  5. Android简单数据存储类SharedPreferences详解及实例(通过“记住密
  6. 2019最新《Android开发全套学习教程》
  7. Android(安卓)Animation学习笔记
  8. Svg标签跟学习
  9. Android学习系列之一

随机推荐

  1. android流畅度比Ios差的原因
  2. Android(安卓)Studio关联source源码
  3. 手写选择题识别-生成编译opencv项目.so文
  4. android View移动的四种方式
  5. Android:如何跟踪第一个Activity界面绘制
  6. [置顶] 前言
  7. Android造成内存泄漏原因及解决方案
  8. Android(安卓)架构演进及未来
  9. Android小知识-自定义View相关知识(1)
  10. [置顶] Android艺术开发探索学习 之 测量