Android滑动菜单简单实现

所谓的滑动菜单就是将一些菜单选项隐藏起来,而不是放置在主屏幕上,然后可以通过滑动的方式将菜单显示出来。如果全靠自己实现的话会非常的困难,幸运的是,谷歌提供了一个DrawerLayout控件

假设我们要实现这样一个功能,点击一个导航按钮,展示滑动菜单

<android.support.v4.widget.DrawerLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/drawer_layout">
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swip_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
<ScrollView
    android:id="@+id/weather_layout"
    android:scrollbars="none"
    android:overScrollMode="never"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <include layout="@layout/title"/>
        <include layout="@layout/now"/>
        <include layout="@layout/forecast"/>
        <include layout="@layout/aqi"/>
        <include layout="@layout/suggestion"/>
    </LinearLayout>
</ScrollView>
    </android.support.v4.widget.SwipeRefreshLayout>
        <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/choose_area_fragment"
            android:name="com.example.haley.coolweather.ChooseAreaFragment"
            android:layout_gravity="start"
            />
    </android.support.v4.widget.DrawerLayout>

DrawerLayout是一个布局,在布局中允许放入两个直接子控件,第一个子空间是主屏幕中,显示的内容,第二个子空间是活动菜单中显示的内容,在这个demo中,我的一个子控件是天气预报的主界面,另一个子控件是一个fragment用于显示省份和城市,接下来再在一个button里面设置一个点击事件

        navButton.setOnClickListener(new  View.OnClickListener(){
            @Override
            public void onClick(View view) {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });

效果展示如图:



猜你喜欢

转载自blog.csdn.net/qq_35647047/article/details/80185495
今日推荐