展会信息港展会大全

用activity实现半透明的、淡入的menu
来源:互联网   发布日期:2015-10-13 07:36:06   浏览:1602次  

导读:用activity实现半透明的、淡入的menu供初学者研究学习虽然android带的menu已经很漂亮,但有的时候也需要我们去自定义menu。开始的时候只是想在android上做一个半透明的menu,但是刚刚接触 android不是太久,我......

用activity实现半透明的、淡入的menu 供初学者研究学习

虽然android带的menu已经很漂亮,但有的时候也需要我们去自定义menu。

开始的时候只是想在android上做一个半透明的menu,但是刚刚接触 android不是太久,我不知道从何处入手,也不知道如何修改android自带的menu,于是到网上找,结果找了好久有关半透明的menu,都没有 找到,只好静下心来研究文档和相关书籍,最后用activity制作出半透明的menu,而且在menu弹出的时候加入了一个淡入的动画。

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

2、创建menu layout

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

<?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" android:gravity="bottom"> <LinearLayout xmlns:android=http: // schemas.android.com/apk/res/android android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="bottom"> < Button android:id="@+id/menu1" android:layout_width="wrap_content" android:layout_height="60dp" android:text="@string/menu1" android:textColor="@drawable/white" android:layout_weight="0.25" android:background="@drawable/buttonselector"> </Button> < Button android:id="@+id/menu2" android:layout_width="wrap_content" android:layout_height="60dp" android:text="@string/menu2" android:textColor="@drawable/white" android:layout_weight="0.25" android:background="@drawable/buttonselector"> </Button> < Button android:id="@+id/menu3" android:layout_width="wrap_content" android:layout_height="60dp" android:text="@string/menu3" android:textColor="@drawable/white" android:layout_weight="0.25" android:background="@drawable/buttonselector"> </Button> < Button android:id="@+id/menu4" android:layout_width="wrap_content" android:layout_height="60dp" android:text="@string/menu4" android:textColor="@drawable/white" android:layout_weight="0.25" android:background="@drawable/buttonselector"> </Button> </LinearLayout> </LinearLayout>

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

<resources> <style name="Theme.Translucent" parent="android:style/Theme.Translucent"> <item name="android:windowBackground">@drawable/translucent_background</item> <item name="android:windowNoTitle"> true</item> <item name="android:colorForeground">#fff</item> </style> </resources>

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,只是出于学习的目的。最后祝大家学习愉快,学习的过程中可以把自己的成果拿出来跟大家分享。谢谢!

赞助本站

人工智能实验室

相关热词: 半透明 淡入

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港