为什么在工具栏中看不到箭头(后退按钮)?

Why cannot see the arrow (back button) in the toolbar?

您好,我正在实现一个工具栏,为此我有以下代码:

Activity

public class SimulationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simulation);
        PDFView pdfView = findViewById(R.id.pdfView);
        pdfView.fromAsset(SIMULATION_PDF).load();

        Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(myToolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                launchMapActivity(this);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

}

布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".simulation.SimulationActivity">


    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/white"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:fontFamily="@font/lato_black"
                android:text="@string/app_name"
                android:textColor="@color/red"
                android:textSize="26sp" />


        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    ...
</android.support.constraint.ConstraintLayout>

问题

如果我在工具栏中添加 TextView,则箭头的工具栏不会显示。无论如何,当我点击箭头位置(它是隐藏的)时,工作完美。

目标

在工具栏中显示文本视图和箭头。

有什么想法吗?

删除工具栏的 TextView child & 设置工具栏标题使用 getSupportActionBar().setTitle()

if (getSupportActionBar() != null)
    getSupportActionBar().setTitle("My title");
  • 要更改工具栏文本颜色,请调用 setTitleTextColor
toolbar.setTitleTextColor(getResources().getColor(R.color.colorName));
  • 为了改变 font-face 在 styles.xml 中添加样式并设置 android:theme="@style/yourStyle"Styles.xml[=25= 中添加以下内容]
<style name="yourStyle" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:fontFamily">@font/yourFont</item>
</style>

最后,问题出在 AppBarLayout 的行中:

android:theme="@style/AppTheme.AppBarOverlay"

现在代码如下:

Activity

public class SimulationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simulation);
        PDFView pdfView = findViewById(R.id.pdfView);
        pdfView.fromAsset(SIMULATION_PDF).load();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                launchMapActivity(this);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

}

布局

<android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:popupTheme="@style/AppTheme.AppBarOverlay">

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textAlignment="center"
                android:fontFamily="@font/lato_black"
                android:text="@string/app_name"
                android:textColor="@color/red"
                android:textSize="26sp"/>

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>