工具栏在创建时自动显示返回箭头。

huangapple go评论82阅读模式
英文:

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.drawerlayout.widget.DrawerLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:id=&quot;@+id/drawer_layout&quot;
    android:fitsSystemWindows=&quot;true&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;

    &lt;LinearLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:orientation=&quot;vertical&quot;&gt;

        &lt;androidx.appcompat.widget.Toolbar
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;?attr/actionBarSize&quot;
            android:background=&quot;@color/gray_95&quot;
            android:id=&quot;@+id/toolbar&quot;
            android:theme=&quot;@style/ThemeOverlay.AppCompat.Dark.ActionBar&quot;
            app:popupTheme=&quot;@style/ThemeOverlay.AppCompat.Light&quot;
            android:elevation=&quot;4dp&quot;&gt;

            &lt;EditText
                android:layout_width=&quot;150dp&quot;
                android:layout_height=&quot;wrap_content&quot;
                    android:hint=&quot;My toolbar&quot;
                android:textColor=&quot;@color/white&quot;&gt;&lt;/EditText&gt;

        &lt;/androidx.appcompat.widget.Toolbar&gt;

        &lt;RelativeLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;&gt;



		// ALL MY  MAIN ACTIVITY XML




         &lt;/RelativeLayout&gt;
    &lt;/LinearLayout&gt;


    &lt;!-- Navigation View --&gt;
    &lt;com.google.android.material.navigation.NavigationView
        android:id=&quot;@+id/nav_view&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;match_parent&quot;
        app:menu=&quot;@menu/drawer_menu&quot;
        android:layout_gravity=&quot;start&quot;
        android:background=&quot;@color/gray_95&quot;
        app:itemTextColor=&quot;@color/white&quot;
        app:itemIconTint=&quot;@color/white&quot;
        app:headerLayout=&quot;@layout/drawer_menu_header&quot;/&gt;

&lt;/androidx.drawerlayout.widget.DrawerLayout&gt;

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);

huangapple
  • 本文由 发表于 2020年9月1日 20:46:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63687974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定