为什么工具栏中的后退箭头未显示? [英] Why is the back arrow in the toolbar not shown?

查看:167
本文介绍了为什么工具栏中的后退箭头未显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My Problem is about the android toolbar. I want to activate the back arrow, to go back to the main menu. I used the expression getSupportActionButton.setDisplayHomeAsUpEnabled(true). When I tested the app, the arrow was not shown. I couldn't find any solutions to solve my problem. When I used the getSupportActionButton.setDefaultDisplayHomeAsUpEnabled(true)-expression, I got the error

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

ActionBar.setDefaultDisplayHomeAsUpEnabled can only be called from within the same library group (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>

推荐答案

如果您使用的是NoActionBar主题,请在其中创建一个工具栏 布局文件.我正在使用新设计支持库的材料组件主题.

If you are using NoActionBar theme then create a toolbar inside layout file. I am using material component theme of new design support library.

布局文件:

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

这篇关于为什么工具栏中的后退箭头未显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆