首页 > 解决方案 > 工具栏在 android 活动上显示为空白

问题描述

我的活动顶部的工具栏显示为空白,但它应该有一个导航自定义图标和一个标题,但两者都不可见?

这是我的xml

<androidx.coordinatorlayout.widget.CoordinatorLayout 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:background="@color/white">


    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:minHeight="?android:attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

这是我的活动

class MyActivity : AppCompatActivity() {

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_me)

        setupToolbar()

    }

    private fun setupToolbar() {
        setSupportActionBar(toolbar)
        toolbar.setNavigationIcon(R.drawable.ic_close)
        toolbar.setNavigationOnClickListener {
            onBackPressed()
        }

        toolbar.setTitle(R.string.profile_unfollow_warning)
        supportActionBar!!.setHomeButtonEnabled(true)
        supportActionBar!!.setDisplayShowCustomEnabled(true)
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        supportActionBar!!.show()
    }

}

我用于此活动的主题

       <activity android:name=".ui.profile.MyActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.MaterialComponents.NoActionBar"/>

标签: androidandroid-toolbar

解决方案


public class MainActivity { 
// Declaring the Toolbar Object
private Toolbar toolbar;

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

    // Attaching the layout to the toolbar object
    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    // Setting toolbar as the ActionBar with setSupportActionBar() call
    setSupportActionBar(toolbar);
}

创建一个像 tool_bar.xml 这样的 xml 文件

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

将工具栏包含到您的 main_activity.xml 中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

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

<TextView
    android:layout_below="@+id/tool_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/TextDimTop"
    android:text="@string/hello_world" />

</RelativeLayout>

推荐阅读