项目地址:
https://github.com/ikew0ng/SwipeBackLayout
一、让需要滑动的Activity基础自定义的style
这里就是为了解决滑动黑屏的问题
<style name="KaleTheme" parent="AppBaseTheme">
<!-- 解决activity切换时的黑屏问题 -->
<item name="android:windowIsTranslucent">true</item>
</style>
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="Theme.AppCompat.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="KaleTheme" parent="AppBaseTheme">
<!-- 解决activity切换时的黑屏问题 -->
<item name="android:windowIsTranslucent">true</item>
</style>
</resources>
我是直接用Application使用了这个样式,仅仅为了演示。
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/KaleTheme" >
二、 用Activity继承一个类
如果你是要兼容Actionbar那么就继承SwipeBackActionbarActivity,这个类是我自己改的,原来的lib中没有。如果不用兼容,那么直接用SwipeBackActivity即可。
三、在方法中找到SwipeBackLayout,并设置滑动的区域和方向
这个就是简单的设置,我直接贴代码了。
package com.kale.swipbacklayouttest;
import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActionbarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* @author:Jack Tony
* @tips :如果要兼容,那么继承SwipeBackActionbarActivity,否则继承SwipeBackActivity
* @date :2014-10-31
*/
public class MainActivity extends SwipeBackActionbarActivity {
private SwipeBackLayout mSwipeBackLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSwipeBackLayout = getSwipeBackLayout();
//设置可以滑动的区域,推荐用屏幕像素的一半来指定
mSwipeBackLayout.setEdgeSize(200);
//设定滑动关闭的方向,SwipeBackLayout.EDGE_ALL表示向下、左、右滑动均可。EDGE_LEFT,EDGE_RIGHT,EDGE_BOTTOM
mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_ALL);
Button btn = (Button)findViewById(R.id.open_button);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
startActivity(new Intent(MainActivity.this,MainActivity.class));
}
});
}
}
上面还添加了一个button,是用来开启新的Activity,主要是便于测试的。