一,主页面的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context="com.example.slidingmenu_reference.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</RelativeLayout>
二,侧滑菜单的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/left_menu"
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"
tools:context="com.example.slidingmenu_reference.SlidingmenuActivity">
</RelativeLayout>
三侧滑菜单fragment
public class LeftFragment extends Fragment {
private static final String TAG = "LeftFragment";
四侧滑菜单fragmnet
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TextView textView=new TextView(getContext());
textView.setText("侧滑菜单");
return textView;
}
}
五主页面
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建侧滑菜单对象
SlidingMenu menu = new SlidingMenu(this);
//设置侧滑菜单的位置
menu.setMode(SlidingMenu.LEFT);
//设置屏幕中可以滑出侧滑菜单的位置
menu.setTouchModeAbove(SlidingMenu.LEFT);
//设置菜单拉出时的颜色蜕变效果
menu.setShadowWidthRes(R.dimen.shadow_width);//颜色蜕变的范围
menu.setShadowDrawable(R.drawable.shadow);
//设置侧滑菜单拉出后,内容区剩余的宽度
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
//设置拉出菜单的透明度
menu.setFadeDegree(0.35f);
//绑定activity.
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
//设置侧滑菜单的布局
menu.setMenu(R.layout.activity_slidingmenu);
//把侧滑菜单替换成fragment,有利于acitvity的解耦
getSupportFragmentManager().beginTransaction().replace(R.id.left_menu, new LeftFragment())
.commit();
}
}