• Android-DrawerLayout介绍


    DrawerLayout已经出来非常久了,个人认为国内的app都深受ios的毒害在设计上都争先模仿ios的风格,都忘了什么是独特的Android风格。自己得先学的然后跟产品争取在项目中使用上一系列的Android风格,碰巧DrawerLayout菜单导航被我争取到了,用到了项目上。

    首先 DrawerLayout存在于v4包中。我们仅仅须要在xml配置就可以

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/hello_world"
            android:textColor="@android:color/black"
            android:gravity="center"/>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#ffffff"
            android:orientation="vertical"/>
    
    </android.support.v4.widget.DrawerLayout>

    在这须要介绍下ActionBarDrawerToggle。控制DrawerLayout的显示/隐藏使用。
    在Activity中的两个回调函数中使用它:
    onConfigurationChanged
    onOptionsItemSelected
    调用ActionBarDrawerToggle.syncState() 在Activity的onPostCreate()中。指示。ActionBarDrawerToggle与DrawerLayout的状态同步。并将ActionBarDrawerToggle中的drawer图标,设置为ActionBar的Home-Button的icon

    public class DrawerActivity extends AppCompatActivity {
    
        DrawerLayout mDrawerLayout;
        ActionBarDrawerToggle toggle;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.test_activity);
    
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
            toggle = new ActionBarDrawerToggle(DrawerActivity.this, mDrawerLayout, R.string.hello_world, R.string.hello_world);
            mDrawerLayout.setDrawerListener(toggle);
    
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("TestDrawerLayout");
        }
    
        @Override
        public void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            toggle.syncState();
        }
    
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            toggle.onConfigurationChanged(newConfig);
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (toggle.onOptionsItemSelected(item))
                return true;
            return super.onOptionsItemSelected(item);
        }
    }

    以上几步设置就可以在项目中成功使用DrawerLayout。可是须要注意几点:
    1.android.support.v4.widget.DrawerLayout是存在于v4包下
    2.主题须要显示ActionBar,比如Theme.AppCompat.Light.DarkActionBar
    3.ActionBarDrawerToggle是个DrawerListener实现,我们能够自己写DrawerListener实现特定的要求。

    我们能够发现独具自带的Android的风格我们使用起来也是非常easy。而我们却深受ios的影响太多了。

  • 相关阅读:
    1015: C语言程序设计教程(第三版)课后习题6.5
    1014 C语言程序设计教程(第三版)课后习题6.4
    1013: C语言程序设计教程(第三版)课后习题6.3
    1012: C语言程序设计教程(第三版)课后习题6.2
    1011 C语言程序设计教程(第三版)课后习题6.1
    链表结点的交换
    int、long、long long取值范围
    windows下XAMPP安装php_memcache扩展
    在VC6.0++ 下的调试
    ubuntu 安装tomcat
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/7229647.html
Copyright © 2020-2023  润新知