【问题标题】:Why is the back arrow in the toolbar not shown?为什么工具栏中的后退箭头不显示?
【发布时间】:2019-07-29 17:13:32
【问题描述】:

我的问题是关于 android 工具栏。我想激活后退箭头,返回主菜单。我使用了表达式getSupportActionButton.setDisplayHomeAsUpEnabled(true)。当我测试该应用程序时,未显示箭头。我找不到任何解决方案来解决我的问题。当我使用getSupportActionButton.setDefaultDisplayHomeAsUpEnabled(true)-表达式时,我得到了错误

ActionBar.setDefaultDisplayHomeAsUpEnabled 只能在同一个库组中调用 (groupId=com.android.support)"。

package de.julius.antgo2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

public class calendar extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(null);
        if(getSupportActionBar() != null){
            getSupportActionBar().setTitle("Test");
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }
    }

}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/apppagebackground"
    android:theme="@style/AppTheme"
    tools:context=".calendar">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"/>

    <LinearLayout
        android:background="#ffffff"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <CalendarView
            android:layout_width="match_parent"
            android:layout_height="match_parent"></CalendarView>

    </LinearLayout>

</RelativeLayout>

【问题讨论】:

    标签: java android xaml android-actionbar android-toolbar


    【解决方案1】:

    我认为日历视图大小隐藏了工具栏,或者主题可能没有操作栏

    【讨论】:

    • 嘿。谢谢您的建议。我删除了日历,但没有帮助。你是对的。我使用了 NoActionBar-Theme,但我认为这是构建自定义标题所必需的。有什么想法吗?
    【解决方案2】:

    下面的代码对我有用

    MainActivity.java

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Toolbar toolbar = findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            if (getSupportActionBar() != null) {
                getSupportActionBar().setTitle("Test");
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setDisplayShowHomeEnabled(true);
            }
        }
    
    
    }
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        android:theme="@style/AppTheme">
    
        <include
            android:id="@+id/toolbar"
            layout="@layout/layout_toolbar" />
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/toolbar"
            android:layout_centerHorizontal="true"
            android:background="#ffffff">
    
            <CalendarView
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
        </LinearLayout>
    
    </RelativeLayout>
    

    styles.xml

    <resources>
    
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>
    
    </resources>
    

    colors.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="colorPrimary">#008577</color>
        <color name="colorPrimaryDark">#00574B</color>
        <color name="colorAccent">#D81B60</color>
    </resources>
    

    layout_toolbar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp" />
    

    如果上面的代码不起作用,请告诉我

    下面是我的输出

    【讨论】:

    • 谢谢,但您的解决方案不起作用。你有其他想法要解决吗?
    • @Julius:我已经更新了答案,请检查并尝试一下。如果不起作用,请告诉我
    • 嘿。谢谢你的努力。一切看起来都像你的版本,但只有箭头不见了。使用的活动是“空活动”。也许有问题。
    【解决方案3】:

    如果您使用的是NoActionBar 主题然后在里面创建一个工具栏 布局文件。我正在使用新设计支持库的材质组件主题。

    布局文件:

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/AppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">
    
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>
    </com.google.android.material.appbar.AppBarLayout>
    

    样式:

    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar" />
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.MaterialComponents.Light" />
    

    Java 文件:

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setTitle("Title");
    
    if (getSupportActionBar() != null) {
           getSupportActionBar().setHomeButtonEnabled(true);
           getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    

    【讨论】:

    • 嘿。我刚试过你的想法。如果我想打开此活动,应用程序会立即崩溃...
    • 您是否忘记将基础应用级别主题更改为材质组件主题并且您已迁移到androidX?
    • 嘿。我得到了和以前一样的错误。我打开活动,应用程序崩溃。
    • 你迁移到androidX了吗?
    • 嘿。我遇到了同样的问题,虽然我迁移到了 androidX。我不知道继续...我尝试使用模板,但除了工具栏图标之外的所有内容都可以使用。空活动或侧导航栏活动。现在删除了没有日历活动和主要活动的所有内容......还有其他想法吗?或者告诉我如何迁移。我以前没做过,可能是我做错了什么。
    猜你喜欢
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多