当按下按钮时,Android DrawerLayout第二个操作栏不会使屏幕变暗

时间:2016-03-08 06:09:54

标签: android android-layout listview navigation-drawer drawerlayout

我创建了一个程序,我的基本导航栏扩展如下:

public class navBar extends AppCompatActivity {}

导航栏代码本身太长而无法包含,但它设置了一个工具栏,并设置了一个导航栏,并且在我的应用程序的大多数部分使用它时效果很好。

然而,当我尝试在扩展类中添加另一个导航栏时,它会做一些奇怪的事情,当我按下兴趣点按钮打开它并运行此代码时:

 public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.POI)
        mDrawerLayout.openDrawer(mDrawerList);
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

按要求打开抽屉,但屏幕不会变暗。像这样: broken

当我用后退按钮/兴趣点按钮关闭屏幕时,例如它稍微滞后,慢慢拖动白色但关闭。

更糟糕的是,当我打开抽屉时 但是,当我用手指从右向左拖动打开抽屉时,它会打开并完美运行。

functional

因为我的地图类也很长,所以我试图只包含重要的代码:

 public class Map2 extends navBar {

private Toolbar toolbar;

ActionBarDrawerToggle mDrawerToggle;
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getLayoutInflater().inflate(R.layout.activity_maptest, frameLayout);
    toolbar = (Toolbar) findViewById(R.id.nav_base_toolbar);
    setSupportActionBar(toolbar);
    toolbar.setNavigationIcon(R.drawable.ic_drawer);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(
            this,
            mDrawerLayout,
            toolbar,
            R.string.drawer_open,
            R.string.drawer_close
    ) {


        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            mDrawerList.clearChoices();
            super.onDrawerClosed(view);
            //getActionBar().setTitle(mTitle);
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            //getActionBar().setTitle(mDrawerTitle);
        }
    };


    mDrawerLayout.setDrawerListener(mDrawerToggle);


    mNavigationDrawerItemTitles= getResources().getStringArray(R.array.navigation_drawer_items_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.right_drawer);
mNavigationDrawerItemTitles= getResources().getStringArray(R.array.navigation_drawer_items_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.right_drawer);
POIAdapter_without_image adapter2 = new POIAdapter_without_image(this, R.layout.points_of_interest, drawerItem2);

    mDrawerList.setAdapter(adapter2);

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());



}
    @Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // If the nav drawer is open, hide action items related to the content view
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    actionBarDrawerToggleChild.syncState();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_skill_view, menu);
    return true;
}

    public void onBackPressed() {
    if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
        mDrawerLayout.closeDrawer(mDrawerList);
    }
    else
        super.onBackPressed();
}

编辑: 每当我在兴趣点列表中选择一个点时,在实际抽屉关闭后的瞬间可以看到拖动的白线: weird_white_bar

当我关上抽屉时,提到的黑色闪光看起来似乎是设置暗淡和不安定的黑色闪光灯。

这让我彻底困惑了几个晚上。谢谢大家!

2 个答案:

答案 0 :(得分:1)

setDrawerShadow 将阴影投射到抽屉

drawer_layout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

更新:

  

drawer_shadow是放在drawable文件夹中的.png文件。

     

START:当抽屉从左侧打开时。

     

结束:当抽屉从右侧打开时

答案 1 :(得分:0)

最后,答案是打电话:

final FrameLayout mapView = (FrameLayout) getLayoutInflater().inflate(R.layout.activity_maptest, frameLayout);

当我想找到任何用途时

 mDrawerLayout2 = (DrawerLayout) mapView.findViewById(R.id.drawer_layout);

例如。

然后需要致电

mDrawerLayout2.closeDrawer(GravityCompat.END);

而不是

mDrawerLayout2.closeDrawer(mDrawerList);

获取所需的功能