android drawLayout抽屉布局

Android drawLayout布局

Android 抽屉布局写法

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:openDrawer="start" >

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:fitsSystemWindows="true"
            android:minHeight="?attr/actionBarSize" />

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>


    <!-- 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. -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/left_drawer"
        android:scrollbars="vertical"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:choiceMode="singleChoice"
        android:divider="@null"
        app:layoutManager="LinearLayoutManager"
        />
</android.support.v4.widget.DrawerLayout>

activity 代码编写

public class CustomNavActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;
    private NavigationView navigationView;
    private ActionBar actionBar;
    private ActionBarDrawerToggle mDrawerToggle;
    private LinearLayout ll_main;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_custom);
        initView();
    }

    private void initView() {
        ll_main = (LinearLayout) findViewById(R.id.ll_main);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        navigationView = (NavigationView) findViewById(R.id.navigationView);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(toolbar);
        actionBar = getSupportActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open,
                R.string.drawer_close);
        mDrawerToggle.syncState();
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        navigationView.setNavigationItemSelectedListener(naviListener);

        // 动态设置自定义menu文本
        Menu menu = navigationView.getMenu();
        MenuItem menuItem = menu.findItem(R.id.menu_findcourse);
        View actionView = MenuItemCompat.getActionView(menuItem);
        TextView tv_num = (TextView) actionView.findViewById(R.id.tv_num);
        tv_num.setText("50");
    }

    private NavigationView.OnNavigationItemSelectedListener naviListener = new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            // 点击NavigationView中定义的menu item时触发反应
            switch (menuItem.getItemId()) {
            case R.id.menu_mycourse:
                menuItem.setChecked(true);
                ll_main.setBackgroundResource(R.color.color1);
                break;
            case R.id.menu_findcourse:
                menuItem.setChecked(true);
                ll_main.setBackgroundResource(R.color.color2);
                break;
            case R.id.menu_setting:
                menuItem.setChecked(true);
                ll_main.setBackgroundResource(R.color.color3);
                break;
            case R.id.menu_downloading:
                menuItem.setChecked(true);
                ll_main.setBackgroundResource(R.color.color4);
                break;
            case R.id.menu_exit:
                menuItem.setChecked(true);
                ll_main.setBackgroundResource(R.color.color5);
                break;
            }
            mDrawerLayout.closeDrawer(navigationView);
            return true;
        }
    };

}

注意这里 toolbar与ActionBarDrawerToggle 进行了绑定,这样会在toolbar中实现动画效果,官方的例子中没有实现这样的效果,官方用的是getActionBar(),要实现这样的效果的方法是getSupportActionBar();

  • 另外build.gradler中是这样的
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:support-v4:25.3.1'
    compile 'com.android.support:design:25.3.1'
}

官方文档
https://developer.android.google.cn/training/implementing-navigation/nav-drawer.html?hl=zh-cn

更加完整的demo下载
http://download.csdn.net/download/qq_26296197/10214046

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值