1. SlidingPaneLayout类
SlidingPaneLayout
实现了左滑菜单,它的第一个子控件就是左滑菜单。
主要方法
public boolean openPane() // 打开菜单
public boolean closePane() // 关闭菜单
// 设置监听事件
public void setPanelSlideListener(PanelSlideListener listener)
public interface PanelSlideListener {
void onPanelSlide(View panel, float slideOffset);
void onPanelOpened(View panel);
void onPanelClosed(View panel);
}
布局文件
<android.support.v4.widget.SlidingPaneLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sliding_pane_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layout_menu"
android:layout_width="120dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="左侧菜单"
android:gravity="center"/>
</LinearLayout>
<View
android:id="@+id/layout_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg"/>
</android.support.v4.widget.SlidingPaneLayout>
SlidingPaneLayout
中内容会随着左侧菜单的移动而移动。效果如下
我们可以设置PanelSlideListener
来自定义移动效果,保持右侧不动,相应缩小。
SlidingPaneLayout slidingPaneLayout = findViewById(R.id.sliding_pane_layout);
final View viewMenu = findViewById(R.id.layout_menu);
final View viewContent = findViewById(R.id.layout_content);
slidingPaneLayout.setPanelSlideListener(new SlidingPaneLayout.SimplePanelSlideListener(){
@Override
public void onPanelSlide(View panel, float slideOffset) {
int width = viewContent.getWidth();
float offsetWidth = viewMenu.getWidth() * slideOffset;
float scale = (width - offsetWidth) / width;
viewContent.setScaleX(scale);
viewContent.setScaleY(scale);
viewContent.setTranslationX(width * (scale - 1) / 2);
}
});
效果如下
2. DrawerLayout类
DrawerLayout
拥有左滑和右滑两种菜单,使用layout_gravity="left|right"
来区分。
主要方法
public void openDrawer(int gravity) // 打开菜单,gravity在菜单中设置
public void closeDrawer(@EdgeGravity int gravity) // 关闭菜单
public void setDrawerLockMode(@LockMode int lockMode) // 设置手势滑动
public static final int LOCK_MODE_UNLOCKED = 0;
public static final int LOCK_MODE_LOCKED_CLOSED = 1; // 默认关闭且锁住,可以通过代码打开
public static final int LOCK_MODE_LOCKED_OPEN = 2; // 默认打开且锁住,可以通过代码关闭
public static final int LOCK_MODE_UNDEFINED = 3;
public void addDrawerListener(@NonNull DrawerListener listener) // 设置监听器
public interface DrawerListener {
void onDrawerSlide(View drawerView, float slideOffset);
void onDrawerOpened(View drawerView);
void onDrawerClosed(View drawerView);
void onDrawerStateChanged(@State int newState);
}
布局文件
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/layout_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg"/>
<LinearLayout
android:layout_width="120dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white"
android:layout_gravity="left">
<TextView
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="左侧菜单"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="120dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white"
android:layout_gravity="right">
<TextView
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="右侧菜单"
android:gravity="center"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
DrawerLayout
中内容不会随菜单的移动而移动,并且支持左右两边菜单。但是只有在靠边缘的位置,才支持手势滑动。效果如下