抽屉式导航栏:将一如既往开幕片 [英] Navigation Drawer: set as always opened on tablets

查看:154
本文介绍了抽屉式导航栏:将一如既往开幕片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用从支持库的抽屉式导航模式: <一href="http://developer.android.com/training/implementing-navigation/nav-drawer.html">http://developer.android.com/training/implementing-navigation/nav-drawer.html

I am using the Navigation Drawer pattern from the support library: http://developer.android.com/training/implementing-navigation/nav-drawer.html

我试图将其设置为总是打开的平板电脑(如侧菜单)

I was trying to set it as always opened on tablet (as a side menu)

时的东西可能与当前实现,还是我们必须创建重复使用相同的code的新布局和新的结构,一个ListView呢?

Is that something possible with the current implementation, or do we have to create a new layout and a new structure with a Listview instead of reusing the same code?

推荐答案

根据大型设备的想法可能有不同的布局文件,我创建了后续项目。

Based on the idea of larger devices could have different layout files, I have created the follow project.

<一个href="https://github.com/jiahaoliuliu/ABSherlockSlides">https://github.com/jiahaoliuliu/ABSherlockSlides

亮点

由于大量装置的抽屉总是可见,这里不需要有一个抽屉。相反,有两个元素具有相同名称的LinearLayout一个就足够了。

Since the drawer of a large device is always visible, there is not need to have an drawer. Instead, a LinearLayout with two elements with the same name will be enough.

<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="horizontal">
     <ListView
             android:id="@+id/listview_drawer"
             android:layout_width="@dimen/drawer_size"
             android:layout_height="match_parent"
             android:layout_gravity="start"
             android:choiceMode="singleChoice"
             android:divider="@android:color/transparent"
             android:dividerHeight="0dp"
             android:background="@color/drawer_background"/>
    <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/drawer_content_padding"
            />
</LinearLayout>

由于我们没有在布局文件的抽屉,当应用程序试图找到在布局中的元素,它会返回null。那么,有没有需要有一个额外的布尔看哪个布局使用。

Because we don't have the drawer in the layout file, when the app try to find the element in the layout, it will return null. So, there is not need to have an extra boolean to see which layout is using.

DrawerLayout mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);

if (mDrawerLayout != null) {
    // Set a custom shadow that overlays the main content when the drawer opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    // Enable ActionBar app icon to behave as action to toggle nav drawer
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    // ActionBarDrawerToggle ties together the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(
            this,
            mDrawerLayout,
            R.drawable.ic_drawer,
            R.string.drawer_open,
            R.string.drawer_close) {

        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
        }

        public void onDrawerOpened(View drawerView) {
            // Set the title on the action when drawer open
            getSupportActionBar().setTitle(mDrawerTitle);
            super.onDrawerOpened(drawerView);
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

下面是该例子使用它作为布尔

Here is the example to use it as boolean.

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    if (mDrawerLayout != null) {
        mDrawerToggle.syncState();
    }
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (mDrawerLayout != null) {
        // Pass any configuration change to the drawer toggles
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
}

这篇关于抽屉式导航栏:将一如既往开幕片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆