注:本篇文章基于我以前发表的文章Android 之 Fragment之上,稍作修改即可达到 炫酷的 滑动效果 :)
<?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/right_fragment" android:name="com.example.myfragments.RightFragment" android:layout_width="match_parent" android:layout_height="match_parent"/>
<?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/left_fragment" android:name="com.example.myfragments.LeftFragment" android:layout_width="match_parent" android:layout_height="match_parent"/>
package com.example.myfragments; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; public class MainActivity extends Activity implements onItemSeletedListener{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); //开始设置滑动菜单 setContentView(R.layout.contentlayout); //设置正文的显示布局 //------------------------------------------------------------------------------------- //NO.1 继承 SlidingActivity 方式 //setBehindContentView(R.layout.menulayout); //设置左侧菜单的布局 //SlidingMenu menu = getSlidingMenu(); //------------------------------------------------------------------------------------- //NO.2 非继承 SlidingActivity 方式 SlidingMenu menu = new SlidingMenu(this); menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT); menu.setMenu(R.layout.menulayout); //添加菜单 //------------------------------------------------------------------------------------- menu.setMode(SlidingMenu.LEFT); //设置菜单位置 menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN); //设置菜单打开方式 //TOUCHMODE_FULLSCREEN 全屏模式 //TOUCHMODE_MARGIN 边缘模式 //TOUCHMODE_NONE 不能通过手势打开 menu.setShadowWidth(12); //设置渐变效果宽度 位于菜单 与 内容 之间 menu.setShadowDrawable(R.drawable.shadow); //设置菜单渐变效果 menu.setFadeDegree(0.9f); //设置淡入程度 当滑出菜单时,渐变效果由0.9 - 0 淡入 menu.setBehindOffset(250); //设置菜单滑出后 距离屏幕右边的长度(直接设置 dp) //------------------------------------------------------------------------------------- //设置结束 //Fragment 一些代码增删改 //添加 //FragmentManager fragmentmanager=getFragmentManager(); //FragmentTransaction fragmenttransaction=fragmentmanager.beginTransaction(); //LeftFragment leftfragment=new LeftFragment(); //fragmenttransaction.add(R.id.left_fragment, leftfragment); //fragmenttransaction.commit(); //删除 //FragmentManager fragmentmanager=getFragmentManager(); //FragmentTransaction fragmenttransaction=fragmentmanager.beginTransaction(); //Fragment leftfragment=fragmentmanager.findFragmentById(R.id.left_fragment); //fragmenttransaction.remove(leftfragment); //fragmenttransaction.commit(); //替换 //FragmentManager fragmentmanager=getFragmentManager(); //FragmentTransaction fragmenttransaction=fragmentmanager.beginTransaction(); //fragmenttransaction.replace(R.id.left_fragment, new LeftFragment()); //fragmenttransaction.commit(); } @Override public void onItemSeleted(int position) { RightFragment rightFragment=(RightFragment) getFragmentManager().findFragmentById(R.id.right_fragment); if(rightFragment != null) rightFragment.update(position); } }
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <gradient android:endColor="@color/darkblue" android:centerColor="@color/darkgray" android:startColor="@color/black" /> </shape>
注:转载请注明出处 :) 毕竟代码是一个一个敲出来的啊,O(∩_∩)O~