Android总结笔记05:Activity的切换方式(从底部弹出,退出时从顶部滑出)
来源:互联网 发布:2016淘宝对刷单的态度 编辑:程序博客网 时间:2024/06/05 06:07
我们都知道Activity默认的切换方式都是左右切换的,下面我们就来学习一下上下式切换的activity.废话不多说,先上图,无图无真相。以下分别是原始状态和activity弹出状态。
一、下面说一下实现思路,当然这个切换方式有好多种实现方式,我这里就说我长用的一种方式,其它方式有兴趣的朋友们可以息行研究一下。我们可以用activity的样式来控制activity的切换样式。
1、用上下滑动的动画。2、Activity的样式是作为一个dialog滑动上来的。3、要在mainfest文件中配置要弹出activity的样式。4、在要弹出activity中设置而已参数。
二、实现代码:
1、包结构:
2、主Activity--> MainActivity.java
package com.jun.activityslidetop;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.View;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.d("fuck:", "Main:===onCreate") ;findViewById(R.id.mybtn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this,Second.class) ;startActivity(intent) ;}}) ;}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overrideprotected void onRestart() {// TODO Auto-generated method stubsuper.onRestart();Log.d("fuck:", "Main:===onRestart") ;}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();Log.d("fuck:", "Main:===onResume") ;}@Overrideprotected void onStop() {// TODO Auto-generated method stubsuper.onStop();Log.d("fuck:", "Main:===onStop") ;}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();Log.d("fuck:", "Main:===onDestroy") ;}@Overrideprotected void onPause() {// TODO Auto-generated method stubsuper.onPause();Log.d("fuck:", "Main:===onPause") ;}}
MainActivity.java只是一个普通的Activity,没有什么难点,只不过我在这里实现了activity的生命周期,为了就是显示弹出activity的时候会调用什么生命周期,咱们在开发中可以注意一下。
3、要弹出的Activity-->Second.java
package com.jun.activityslidetop;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.ViewGroup.LayoutParams;public class Second extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.second) ;//注意这里要有这句话,不然弹出的布局不是理想中的。getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);this.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {finish() ;}}) ;}}
这个activity也没有什么特别的,只是一个设置布局参数和返回按钮事件的处理。当然想要弹出肯定不是这样的,这样的和普通的activity的切换方式有什么区别呢。
4、下面就要看真正的activity的模式了这里是决定activity切换的重点。res/styles.xml
<resources> <!-- Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.xml on newer devices. --> <style name="AppBaseTheme" parent="android:Theme.Light"> <!-- Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. --> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style> <!-- 以下的解决了弹出弹入的问题 --> <style name="popup_dialog_anim" parent="@style/popup_dialog"> <item name="android:windowAnimationStyle">@style/dialog_animations</item> </style> <style name="popup_dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <!--Dialog的windowFrame框为无 --> <item name="android:windowIsFloating">true</item><!-- 是否浮现在activity之上 --> <item name="android:windowIsTranslucent">true</item><!-- 是否半透明 --> <item name="android:windowNoTitle">true</item><!-- 是否显示title --> <item name="android:background">@android:color/transparent</item> <item name="android:windowBackground">@android:color/transparent</item> <!-- 设置dialog的背景 --> <item name="android:backgroundDimEnabled">true</item><!-- 背景是否模糊显示 --> <item name="android:backgroundDimAmount">0.6</item> </style> <style name="dialog_animations"> <item name="android:windowEnterAnimation">@anim/slide_in_from_bottom</item> <!-- 从下面弹出时的动画 --> <item name="android:windowExitAnimation">@anim/slide_out_from_bottom</item> <!-- 从上面下滑到底部时的动画 --> </style> <!-- --> </resources>
以上的xml注释很详细不用多说,下面看一下弹出和离开的动画。在res/anim目录下。
(1)、slide_in_from_bottom.xml(从底部弹出的动画)
<?xml version="1.0" encoding="utf-8" standalone="no"?><translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromXDelta="0.0%" android:fromYDelta="100.0%" android:toXDelta="0.0%" android:toYDelta="0.0%" />
(2)、slide_out_from_bottom.xml(从顶部滑出的动画)
<?xml version="1.0" encoding="utf-8" standalone="no"?><translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromXDelta="0.0%" android:fromYDelta="0.0%" android:toXDelta="0.0%" android:toYDelta="100.0%" />
5、最后别忘了在mainfext.xml中需要弹出的activity上设置上所定义的样式即可如:Second是弹出式的那就如下设置。
<activity android:name=".Second" android:theme="@style/popup_dialog_anim" android:configChanges="keyboardHidden|orientation"></activity>
注意:这里有两点值得得注意,第一:就是要在mainfest中设置需要弹出的activity的样式,第二是:要在弹出的activity的setcontent()后面写上设置布局参数的代码getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);不然布局加载出来有问题,你可以试一试去年这一行。
三、由于弹出的activity是dialog样式的,所以当我们离开second.java时调用MainActivity中的onPuase()方法,再次弹出时调用onResume()方法。和普通的activity不太一样。这里说明一点Dialog样式的activity和Dialog是不一样的,如果我们弹出一个dialog的话,什么生命周期方法都不会调用的,除非你的dialog上面有处理activity的逻辑(比如点击确认按键打开一个新的activity)。
好了今天的内容就到这里了,如果朋友们有什么好的建议和意见都提出来我们一同进步。
最后附上源码下载地址:http://download.csdn.net/detail/android0012345/6461065
- Android总结笔记05:Activity的切换方式(从底部弹出,退出时从顶部滑出)
- android Activity实现从底部弹出或滑出选择菜单或窗口
- android Activity实现从底部弹出或滑出选择菜单或窗口
- android Activity实现从底部弹出或滑出选择菜单或窗口
- android Activity实现从底部弹出或滑出选择菜单或窗口
- android Activity实现从底部弹出或滑出选择菜单或窗口
- android Activity实现从底部弹出或滑出选择菜单或窗口
- android Activity实现从底部弹出或滑出选择菜单或窗口
- Notification从顶部慢慢滑出弹出
- android style 从底部弹出或滑出选择菜单窗口的样式
- activity从底部弹出动画
- android 从顶部弹出的SnackBar
- Android从底部弹出的多选弹窗
- Snackbars从顶部滑出的实现
- 透明Activity从底部弹出,除去顶部黑线和系统状态栏
- android Activity实现从底部弹出菜单或窗口
- 从底部弹出或滑出选择菜单或窗口
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- Android桌面悬浮窗效果实现,仿360手机卫士悬浮窗效果(转)
- java中并发包简要分析01
- 大学英语六级听力——005(Describing people )
- jQuery
- jQuery
- Android总结笔记05:Activity的切换方式(从底部弹出,退出时从顶部滑出)
- 解决win7、win8下不能双击运行Jar包
- 前后台系统的低功耗编程思想——STM8平台
- Linux更改Apache网站目录出错:Document root must be a directory
- 一道JAVA 21点纸牌游戏
- Dojo与jQuery综合比较分析
- 如何学好VC和MFC(转)
- hashmap
- 动态代理、反射中的Exception