英文:
Toolbar automatically showing back arrow when created
问题
我已经创建了一个名为 MainActivity
的活动,在其中使用了一个 DrawerLayout
。
XML 文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/gray_95"
android:id="@+id/toolbar"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp">
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:hint="My toolbar"
android:textColor="@color/white"></EditText>
</androidx.appcompat.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 所有我的主活动 XML 部分 -->
</RelativeLayout>
</LinearLayout>
<!-- 导航视图 -->
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/drawer_menu"
android:layout_gravity="start"
android:background="@color/gray_95"
app:itemTextColor="@color/white"
app:itemIconTint="@color/white"
app:headerLayout="@layout/drawer_menu_header"/>
</androidx.drawerlayout.widget.DrawerLayout>
在这个 Activity
中,我已经创建了启动抽屉的基本代码,该代码在 onCreate(Bundle savedInstanceState)
中被调用,并且重写了 onNavigationItemSelected(@NonNull MenuItem item)
方法。
JAVA
private void setDrawerLayout() {
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle mToggle = new ActionBarDrawerToggle(this, mDrawerLayout , toolbar, R.string.open_drawer, R.string.close_drawer);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerLayout.addDrawerListener(mToggle);
}
导航功能是正常的,但我遇到了一个错误。如上述的 XML
文件所示,我已经将整个 Activity
添加到了 Toolbar
下方。我猜测我做错了什么,应该是我应该创建一个 Fragment
,并将下面这些代码添加到 onCreate
方法中:
getSupportFragmentManager().beginTransaction().replace(R.id.MYMAINFRAGMENT, new SettingFragment()).commit();
但在这之前,我想确认一下,这是否是问题所在,因为这会让我花费很多时间(项目已经很复杂,我需要做很多修改)。
我附上一张图片来展示当前的解决方案:
正如您所看到的,当我启动应用程序时,返回按钮会显示出来。当我点击它时,它只是打开了抽屉。可能发生了什么情况?我需要创建一个 MAINFRAGMENT
吗?
英文:
I have created a MainActivity
where I have a DrawerLayout
XML File
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/gray_95"
android:id="@+id/toolbar"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp">
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:hint="My toolbar"
android:textColor="@color/white"></EditText>
</androidx.appcompat.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
// ALL MY MAIN ACTIVITY XML
</RelativeLayout>
</LinearLayout>
<!-- Navigation View -->
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/drawer_menu"
android:layout_gravity="start"
android:background="@color/gray_95"
app:itemTextColor="@color/white"
app:itemIconTint="@color/white"
app:headerLayout="@layout/drawer_menu_header"/>
</androidx.drawerlayout.widget.DrawerLayout>
On this Activity
I have created the basic code that is required to making work the Drawer
which is called when from onCreate(Bundle savedInstanceState)
and overrided onNavigationItemSelected(@NonNull MenuItem item)
JAVA
private void setDrawerLayout() {
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle mToggle = new ActionBarDrawerToggle(this, mDrawerLayout , toolbar, R.string.open_drawer, R.string.close_drawer);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerLayout.addDrawerListener(mToggle);
}
The Navigation
works, but I am dealing with an error. As the previous XML
file shows, I have added the whole Activity
under the Toolbar
. I am assuming that what I am doing wrong is that instead of the activity, I should create a Fragment
and add the following lines of code to onCreate
method:
getSupportFragmentManager().beginTransaction().replace(R.id.MYMAINFRAGMENT, new SettingFragment()).commit();
but before doing that, I wanted to confirm if this is the issue or not, since it would make me to spend a lot of time on it (the project is advanced, and I would need to make tons of changes)
I attach a picture to show the current solution
As you can see, as soon as I launch the application the back button is shown. When I click on it, it simply opens the drawer. What could be happening? Do I need to create a MAINFRAGMENT
?
答案1
得分: 1
删除这一行:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
英文:
Remove this line:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论