DrawerLayout的入门

来源:互联网 发布:才真旺姆解释双修 知乎 编辑:程序博客网 时间:2024/06/07 04:42

DrawerLayout学习总结:
1、关于布局:
贴代码
drawer_layout\content_frame\left_drawer 三部分组成

<android.support.v4.widget.DrawerLayout                            xmlns:android="http://schemas.android.com/apk/res/android"                            android:id="@+id/drawer_layout1"                            android:layout_width="match_parent"                            android:layout_height="match_parent">                            <!-- As the main content view, the view below consumes the entire                                 space available using match_parent in both dimensions. -->                            <FrameLayout                                android:id="@+id/content_frame1"                                android:layout_width="match_parent"                                android:layout_height="match_parent" />                            <!-- android:layout_gravity="start" tells DrawerLayout to treat                                 this as a sliding drawer on the left side for left-to-right                                 languages and on the right side for right-to-left languages.                                 The drawer is given a fixed width in dp and extends the full height of                                 the container. A solid background is used for contrast                                 with the content view. -->                            <ListView                                android:id="@+id/left_drawer1"                                android:layout_width="240dp"                                android:layout_height="match_parent"                                android:layout_gravity="start"                                android:choiceMode="singleChoice"                                android:divider="@android:color/transparent"                                android:dividerHeight="0dp"                                android:background="#111"/>                        </android.support.v4.widget.DrawerLayout>

2、关于实现ActionBarDrawerToggle实例化一个ActionBarDrawerToggle 将 toggle设置为DrawerLayout的监听器 →
drawerLayout.setDrawerListener(toggle);
如果在toggle内改变了actionbar 应该 在监听方法内实现invalidateOptionsMenu()方法
3、 左上方的图标显示并可用
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

并在onOptionsItemSelected(MenuItem item)内增加语句
if (toggle.onOptionsItemSelected(item)) return true;
<绑定homebutton的点击事件,这样才能控制做左侧菜单的拉开和关闭 >
可以监听到左上角图标的点击事件;并可实现点击拉动左侧菜单;
4、官方建议实现以下方法

@Override    protected void onPostCreate(Bundle savedInstanceState) {        super.onPostCreate(savedInstanceState);        toggle.syncState();//如果执行该方法,则会告诉activity重新执行onConfigurationChanged()方法    }    @Override    public void onConfigurationChanged(Configuration newConfig) {        super.onConfigurationChanged(newConfig);        toggle.onConfigurationChanged(newConfig);//重新配置toggle 显示新图标    }

可以显示改动的图标;并重新配置
有时间把DrawerLayout的实现代码贴出来;

0 0
原创粉丝点击