我遇到一个奇怪的问题。我在抽屉式布局中创建了一个导航视图,但是一旦活动开始,导航视图才可见。工具栏的汉堡菜单不可单击。
main_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view"
>
<group android:checkableBehavior="single">
<item android:id="@+id/menu_explore" android:title="@string/explore"
android:icon="@drawable/exploreicon" />
<item android:id="@+id/menu_profile" android:title="@string/profile"
android:icon="@drawable/profileicon"/>
<item android:id="@+id/menu_search" android:title="@string/search"
android:icon="@drawable/searchicon"/>
<item android:id="@+id/menu_settings" android:title="@string/settings"
android:icon="@drawable/settingsicon"/>
<item android:id="@+id/menu_bookmarked" android:title="@string/bookmarked"
android:icon="@drawable/bookmarkicon"/>
<item android:id="@+id/menu_logout" android:title="@string/log_out"
android:icon="@drawable/logouticon"/>
</group>
</menu>
activity_main.xml
<androidx.drawerlayout.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/drawerLayout"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:layout_gravity="start"
android:clickable="false"
android:clipToPadding="false"
android:fitsSystemWindows="true"
android:padding="16dp"
android:scrollIndicators="start"
android:visibility="visible"
tools:openDrawer="start">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/mainToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#FFF"
app:headerLayout="@layout/menu_header"
app:itemIconTint="#6b6b6b"
app:itemTextColor="#6b6b6b"
app:menu="@menu/main_menu">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
主要活动的风格:
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
主要活动:
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.mainToolbar);
toolbar.setTitle(R.string.app_name);
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawerLayout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,
R.string.open,R.string.close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
}
}
我希望仅在单击工具栏中的汉堡包图标时才显示导航视图。我遵循的每个教程都对我做同样的事情,但是我的并没有显示在工具栏内。
答案 0 :(得分:1)
在toolbar
启动期间传递ActionBarDrawerToggle
实例
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout, toolbar, R.string.open,R.string.close);