不多说直接上代码
Main布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.lenovo.day27_slidingfragmentmenu.MainActivity">
<!--主界面-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
<!--左侧fragment-->
<fragment
android:id="@+id/left"
android:name="com.example.lenovo.day27_slidingfragmentmenu.FragmentLeft"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--右侧fragment-->
<fragment
android:id="@+id/right"
android:name="com.example.lenovo.day27_slidingfragmentmenu.FragmentRight"
android:layout_width="match_parent"
android:layout_height="match_parent"></fragment>
</LinearLayout>
Fragment和fragment_layout没啥技术含量,就把主要创建的.java和Layout传上
Activity代码
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
public class MainActivity extends SlidingFragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 设为左侧视图
setBehindContentView(R.layout.left_layout);
SlidingMenu menu = getSlidingMenu();
// 设置为左右滑动模式
menu.setMode(SlidingMenu.LEFT_RIGHT);
// 设置右侧视图
menu.setSecondaryMenu(R.layout.right_layout);
// 滑出宽度
menu.setBehindWidth(menu.getResources().getDisplayMetrics().widthPixels / 3);
// 在绑定容器
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// main_layout的R.id
transaction.replace(R.id.left, new FragmentLeft());
transaction.replace(R.id.right, new FragmentRight());
transaction.commit();
}
}