![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL2ATOwQzMwATM5EDNwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
1、首先是右→左進入動畫 、slide_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="800"
android:fromXDelta="200%"
android:toXDelta="0" />
</set>
左→右退出動畫、slide_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="800"
android:fromXDelta="0"
android:toXDelta="200%" />
</set>
2、activity 的xml代碼
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/mainks"
android:layout_width="200dp"
android:layout_height="100dp"
android:gravity="center"
android:text="開始"/>
<TextView
android:id="@+id/maingb"
android:layout_width="200dp"
android:layout_height="100dp"
android:gravity="center"
android:text="關閉"/>
</LinearLayout>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/clRootView"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.design.widget.CoordinatorLayout>
</FrameLayout>
3、activity實作方法
boolean ifshowfragment = false;
抽屜fragment進入方法yhox
private void showAboutFragment() {
getSupportFragmentManager()
.beginTransaction()
.disallowAddToBackStack()
.setCustomAnimations(R.anim.slide_left, R.anim.slide_right)
.add(R.id.clRootView, MyFragment.newInstance(), MyFragment.TAG)
.commit();
ifshowfragment = true;
}
抽屜fragment退出方法
public void onFragmentDetached(String tag) {
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(tag);
if (fragment != null) {
fragmentManager
.beginTransaction()
.disallowAddToBackStack()
.setCustomAnimations(R.anim.slide_left, R.anim.slide_right)
.remove(fragment)
.commitNow();
}
ifshowfragment = false;
}
activity傳回鍵退出動畫方法:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == KeyEvent.ACTION_DOWN) {
if (ifshowfragment) {
onFragmentDetached(MyFragment.TAG);
return true;
}
}
return super.onKeyDown(keyCode, event);
}
demo雲盤連結:https://pan.baidu.com/s/1t__nXXnfyEUVA24gPOKRAQ
線上回複密碼QQ:1085220040
半屏導航fragment實作方法
1、添加導航view依賴
compile 'com.android.support:design:28.0.0'
2、activity代碼:
private NavigationView navigationView;
private DrawerLayout drawerLayout;
navigationView = (NavigationView)findViewById(R.id.navigation_view);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer);
drawerLayout.openDrawer(navigationView);
drawerLayout.closeDrawer(navigationView);
效果:
全屏 半屏