Android抽屉在平板电脑中打开,但主要内容被禁用

时间:2015-09-07 10:44:45

标签: android android-layout navigation-drawer

我想在平板电脑土地模式下打开抽屉。但是我使用此代码

RelativeLayout frameLayout = (RelativeLayout)findViewById(R.id.drawer_main_content);
    if(((ViewGroup.MarginLayoutParams)frameLayout.getLayoutParams()).leftMargin == (int)getResources().getDimension(R.dimen.drawer_size)) {
        Drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN, mRecyclerView);

        Drawer.setScrimColor(Color.TRANSPARENT);

        isDrawerLocked = true;
    }else {
        mDrawerToggle.syncState();
    }

但我的主要内容是禁用。我在代码下面使用它只能清楚地查看主要内容,但我无法点击主要内容中的按钮。

Drawer.setScrimColor(Color.TRANSPARENT);

这是我的完整java代码和xml

    <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/DrawerLayout_in_applications"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="7dp"
    >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/drawer_RecyclerView_in_applications"
        android:layout_width="@dimen/drawer_size"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:choiceMode="singleChoice"
        android:background="#ffffff"
        android:scrollbars="vertical">
    </android.support.v7.widget.RecyclerView>
    <RelativeLayout
        android:id="@+id/drawer_main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="@dimen/drawer_content_padding"
        android:background="@drawable/backgrounddock"

        >
        <include
            android:id="@+id/tool_bar"
            layout="@layout/tool_bar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            />

<Button android:layout_width="match_parent"
        android:layout_height="match_parent"

 />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>

Java代码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(null != savedInstanceState){
        }
        setContentView(R.layout.activity_grid_docker);
        toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
        setSupportActionBar(toolbar);
        if(Build.VERSION.SDK_INT > 19) {
            toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
        }

        getSupportActionBar().setTitle(AppController.getInstance().getUname());
        dat=new ArrayList<DrawerListItem>();
        DrawerListItem item=new DrawerListItem();
        item.setName("Home");
        item.setId("home");
        item.setImg(R.drawable.ic_home_black_48dp);
        dat.add(item);
        DrawerListItem item1=new DrawerListItem();
        item1.setName("Setting");
        item1.setId("set");
        item1.setImg(R.drawable.ic_settings_black_48dp);
        dat.add(item1);
        DrawerListItem item2=new DrawerListItem();
        item2.setName("SignOut");
        item2.setId("signout");
        item2.setImg(R.drawable.ic_power_settings_new_black_48dp);
        dat.add(item2);
//        toolbar.setNavigationIcon(R.drawable.ic_check_white_18dp);

        //start drawer
        mRecyclerView = (RecyclerView) findViewById(R.id.drawer_RecyclerView_in_applications); // Assigning the RecyclerView Object to the xml View
        mRecyclerView.setHasFixedSize(true);                            // Letting the system know that the list objects are of fixed size
        mAdapter = new DrawerAdapter(dat,NAME,EMAIL,PROFILE,this,this,mRecyclerView);       // Creating the Adapter of MyAdapter class(which we are going to see in a bit)
        // And passing the titles,icons,header view name, header view email,
        // and header view profile picture
        mRecyclerView.setAdapter(mAdapter);                              // Setting the adapter to RecyclerView
        mLayoutManager = new LinearLayoutManager(this);                 // Creating a layout Manager
        mRecyclerView.setLayoutManager(mLayoutManager);                 // Setting the layout Manager
        Drawer = (DrawerLayout) findViewById(R.id.DrawerLayout_in_applications);

        mDrawerToggle = new ActionBarDrawerToggle(this,Drawer,toolbar,R.string.openDrawer,R.string.closeDrawer){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);


            }
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };
        Drawer.setDrawerListener(mDrawerToggle); // Drawer Listener set to the Drawer toggle

        RelativeLayout frameLayout = (RelativeLayout)findViewById(R.id.drawer_main_content);
        if(((ViewGroup.MarginLayoutParams)frameLayout.getLayoutParams()).leftMargin == (int)getResources().getDimension(R.dimen.drawer_size)) {
            Drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN, mRecyclerView);

 Drawer.setScrimColor(Color.TRANSPARENT);
            isDrawerLocked = true;
        }else {
            mDrawerToggle.syncState();
        }
//



    }

1 个答案:

答案 0 :(得分:0)

这是因为您正在使用LOCK_MODE_LOCKED_OPEN使抽屉在锁定状态下打开。

您应首先检查方向然后您可以使用抽屉做任何事情,如:

boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;

if(isLandscape) {
    // your tablet is in landscape mode
    // so unlock drawer 
    drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
} else {
    // your tablet is in Portrait mode so lock drawer so user cannot open it
    drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}

此外,您应添加listener以获取屏幕方向更改。因为您会根据orientation更改频繁锁定和解锁抽屉。为此我们可以使用OrientationEventListener类。说明如下:https://developer.android.com/reference/android/view/OrientationEventListener.html

OrientationEventListener mListener = new OrientationEventListener(this,SensorManager.SENSOR_DELAY_NORMAL) {

@Override
public void onOrientationChanged(int arg0) {

    if(arg0 == OrientationEventListener.ORIENTATION_UNKNOWN) {
        // orientation is unknown so do nothing
        return;
    }
    if((345 < arg0 && arg0 < 359) || (arg0 >= 0 && arg0 < 45) || (arg0 < 180 && 135 < arg0) || ( arg0 < 215 && arg0 > 180)) {
        // device is Portrait
        drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    } else {
        // device is Landscape
        drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
    }
}

});

希望这会对你有所帮助