实现滑动效果可能有多种方法,比如SliddingDrawer,NavigationDrawer,SliddingMenu,DrawerLayout等
本文实现一个简单的滑动抽屉效果的demo(android studio 环境)
上代码:
xml布局代码:
<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">
<!-- 抽屉隐藏时显示的布局 S-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="课程表"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
<!-- 抽屉隐藏时显示的布局 E-->
<!--抽屉里的布局在这里写 S-->
<LinearLayout
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
android:fitsSystemWindows="true"
android:orientation="vertical"
>
<ListView android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</LinearLayout>
<!--抽屉里的布局在这里写 E-->
</android.support.v4.widget.DrawerLayout>
以上的布局可以根据需要自己改动,第一个布局是在未显示抽屉时要显示的界面,第二个布局是抽屉滑出的界面
MainActivity.java
package slidingdrawer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
源码下载
效果图