Android - DrawerLayout和工具栏在第一次打开时不可见
我目前正在开发一个使用工具栏和DrawerLayout的android应用程序。主要内容是一个自定义的SurfaceView,我可以在其中绘制不同的形状,文本......左侧菜单是一个NavigationView并用作工具箱(我选择我想从左侧绘制的图形,并在SurfaceView上绘制它)。除了一件事情之外,一切工作都正常:当我第一次尝试打开左侧菜单时(通过单击工具栏或从屏幕左侧滑动),项目不可见。虽然我没有点击任何物品(不可见),但它们保持隐形状态。只有当我点击一个不可见的项目时,问题才得以解决,之后菜单正常工作。我使用的是自定义主题,以隐藏状态栏,并删除默认的动作条:Android - DrawerLayout和工具栏在第一次打开时不可见
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.NoActionBar"></style>
<style name="ColladiaTheme" parent="AppBaseTheme">
<!-- Remove action bar -->
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<!-- Remove status bar -->
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<!-- Material theme -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
下面是一些截图:
Menu visible after clicking an item
<?xml version="1.0" encoding="utf-8"?>
<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" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.ia04nf28.colladia.DrawColladiaView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/draw_view" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>
</FrameLayout>
<!-- Left navigation menu -->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:layout_gravity="start"
android:fitsSystemWindows="false"
app:menu="@menu/nav_view_items" >
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
这里是活动代码:
public class DrawActivity extends AppCompatActivity {
private static final String TAG = "DrawActivity";
private DrawerLayout drawer;
ActionBarDrawerToggle drawerToggle;
private NavigationView nav;
private DrawColladiaView colladiaView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draw);
// Change toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
colladiaView = (DrawColladiaView) findViewById(R.id.draw_view);
colladiaView.setApplicationCtx(getApplicationContext());
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// Add burger button
drawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(drawerToggle);
// Removes overlay
drawer.setScrimColor(Color.TRANSPARENT);
drawer.closeDrawers();
nav = (NavigationView) findViewById(R.id.nav_view);
setUpDrawerContent(nav);
}
public void setUpDrawerContent(NavigationView nav)
{
nav.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
selectDrawerItem(item);
return true;
}
}
);
}
public void selectDrawerItem(MenuItem item)
{
switch(item.getItemId())
{
case R.id.nav_home:
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
break;
default:
Element newElement = ElementFactory.createElement(getApplicationContext(), item.getTitle().toString());
if (newElement != null) colladiaView.insertNewElement(newElement);
drawer.closeDrawers();
break;
}
}
@Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void onBackPressed() {
Log.d(TAG, "onBackPressed");
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
看起来你应该重写onOptionsItemSelected
,而不是onBackPressed
:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// This will toggle the drawer if android.R.id.home is clicked
if(drawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle any other menu item selections...
return super.onOptionsItemSelected;
}
说实话,我没有看到你的抽屉是不断被打开的位置。它应该仍然响应从左边的滑动。当点击android.R.id.home
(后退/菜单按钮)时,此代码会将其设置为切换。
好的,问题出在setUpDrawerContent和selectDrawerItem中。 所以,你基本上想要设置按钮,但是你需要添加一个navigationItemSelectedListener。 发生这种情况: 您准备好所有内容,然后添加侦听器。直到您单击导航器抽屉中的项目才会发生任何事情。当你点击某个东西时,点击侦听器会触发,并且抽屉项目被创建。
您尝试创建动态元素,但只有当您按下不可见按钮时才会触发此创建。
尝试移动从交换机的默认情况下,代码selectDrawerItem到setUpDrawerContent,并将其放置在你面前的几行设置navigationItemSelectedListener。您还需要在那里创建所有按钮,使用for循环或其他内容来预先创建菜单项。
像这样:
public void setUpDrawerContent(NavigationView nav)
{
/*
//for loop here to add your menu items
Element newElement = ElementFactory.createElement(getApplicationContext(), item.getTitle().toString());
if (newElement != null)
colladiaView.insertNewElement(newElement);
*/
nav.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
selectDrawerItem(item);
return true;
}
}
);
}
public void selectDrawerItem(MenuItem item)
{
switch(item.getItemId())
{
case R.id.nav_home:
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
break;
// more cases goes here to handle each menu item clicks
default:
//pop a Toast message or something about not existing function
drawer.closeDrawers();
break;
}
}
的ElementFactory部分不与在菜单中的项目。navigationView中的可用项目是通过以下布局设置的:app:menu =“@ menu/nav_view_items” – Bash
没有跳出以我为你的XML不正确,但也可能是跟你的活动代码。 – Bryan
@Bryan,我编辑了我的问题并添加了我的活动代码 – Bash