无法在 android 中设置工具栏

Cannot set the toolbar in android

我正在尝试添加自定义工具栏

这是我的toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:subtitleTextColor="@color/sub_text_color"
app:navigationContentDescription="@string/abc_action_bar_up_description"
android:background="@color/sub_text_color"
app:navigationIcon="?attr/homeAsUpIndicator"
/>

这是我的风格

   <!-- 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>

我已将其包含在我的 activity 主

<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"
tools:context="com.example.myproj.activities.RegisterActivity">

<include layout="@layout/toolbar"/>

在我的主要 activity class 我打电话给

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

    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setTitle(R.string.create_account);
}

但我仍然无法在 class 中看到工具栏。 谁能帮我找出错误

Try this, 

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/appbarlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">

            <com.itc.classmate.utils.FontTextView
                android:id="@+id/textview_toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="@dimen/gap_btn_logo"
                android:layout_toLeftOf="@+id/rel_toolbar_options"
                android:ellipsize="marquee"
                android:paddingRight="5dp"
                android:singleLine="true"
                android:text=""
                android:textColor="@color/colorWhite"
                android:textSize="@dimen/comm_text_size_normal" />


            <LinearLayout
                android:id="@+id/rel_toolbar_options"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toLeftOf="@+id/rel_toolbar_right_cornor_item"
                android:gravity="center_vertical">

                <ImageView
                    android:id="@+id/imgview_toolbar_share"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginRight="@dimen/toolbar_notification_right_margin"
                    android:layout_toLeftOf="@+id/imgview_toolbar_choose_class"
                    android:padding="@dimen/inc_toolbar_icon_padding"
                    android:src="@drawable/ic_menu_share"
                    android:visibility="gone" />

                <ImageView
                    android:id="@+id/imgview_toolbar_choose_class"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginRight="@dimen/toolbar_notification_right_margin"
                    android:layout_toLeftOf="@+id/imgview_toolbar_edit"
                    android:padding="@dimen/inc_toolbar_icon_padding"
                    android:src="@drawable/selectclass"
                    android:visibility="gone" />

                <ImageView
                    android:id="@+id/imgview_toolbar_edit"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginRight="@dimen/toolbar_notification_right_margin"
                    android:layout_toLeftOf="@+id/imgview_toolbar_done"
                    android:padding="@dimen/inc_toolbar_icon_padding"
                    android:src="@drawable/profile_edit_icon"
                    android:visibility="gone" />

                <ImageView
                    android:id="@+id/imgview_toolbar_done"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginRight="14dp"
                    android:layout_toLeftOf="@+id/textview_toolbar_delete_bookmark"
                    android:padding="@dimen/inc_toolbar_icon_padding"
                    android:src="@drawable/done"
                    android:visibility="gone" />

                <com.itc.classmate.utils.FontTextView
                    android:id="@+id/textview_toolbar_delete_bookmark"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginRight="14dp"
                    android:layout_toLeftOf="@+id/imageview_toolbar_delete"
                    android:padding="@dimen/inc_toolbar_icon_padding"
                    android:text="@string/clear_all"
                    android:textColor="@color/colorWhite"
                    android:textSize="@dimen/comm_text_size_mediam"
                    android:visibility="gone" />

                <ImageView
                    android:id="@+id/imageview_toolbar_delete"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginRight="14dp"
                    android:layout_toLeftOf="@+id/textview_toolbar_done"
                    android:padding="@dimen/inc_toolbar_icon_padding"
                    android:src="@drawable/delete"
                    android:visibility="gone" />


                <com.itc.classmate.utils.FontTextView
                    android:id="@+id/textview_toolbar_done"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginRight="@dimen/toolbar_notification_right_margin"
                    android:layout_toLeftOf="@+id/imgview_toolbar_notification"
                    android:padding="@dimen/inc_toolbar_icon_padding"
                    android:text="Done"
                    android:textColor="@color/colorWhite"
                    android:textSize="@dimen/comm_text_size_normal"
                    android:visibility="gone"
                    app:textAllCaps="true" />

                <RelativeLayout
                    android:id="@+id/rl_notification"
                    android:layout_width="wrap_content"
                    android:layout_height="?actionBarSize"
                    android:layout_marginRight="@dimen/toolbar_notification_right_margin"
                    >
                    <ImageView
                        android:id="@+id/imgview_toolbar_notification"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:padding="@dimen/inc_toolbar_icon_padding"
                        android:src="@drawable/bell"
                        android:visibility="gone" />
                    <TextView
                        android:id="@+id/txtview_toolbar_notification"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/colorWhite"
                        android:background="@drawable/shape_notification_dot"
                        android:textSize="@dimen/comm_text_size_very_small"
                        android:padding="2dp"
                        android:gravity="center"
                        android:visibility="gone"
                        android:layout_marginTop="10dp"
                        android:layout_marginStart="10dp"/>
                </RelativeLayout>
            </LinearLayout>

            <RelativeLayout
                android:id="@+id/rel_toolbar_right_cornor_item"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerInParent="true"
                android:layout_marginRight="@dimen/toolbar_notification_right_margin"
                android:padding="@dimen/inc_toolbar_icon_padding"
                android:visibility="visible">

                <com.itc.classmate.utils.CircularImage
                    android:id="@+id/imgview_toolbar_profile"
                    android:layout_width="@dimen/toolbar_profile_width_height"
                    android:layout_height="@dimen/toolbar_profile_width_height"
                    android:layout_centerInParent="true"
                    android:src="@drawable/profile_icon"
                    android:visibility="invisible" />

                <com.itc.classmate.utils.FontTextView
                    android:id="@+id/textview_toolbar_save"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="@string/save"
                    android:textColor="@color/colorWhite"
                    android:textSize="@dimen/comm_text_size_mediam"
                    android:visibility="gone"
                    app:textAllCaps="true" />

                <ImageView
                    android:id="@+id/imgview_toolbar_delete"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_marginRight="@dimen/toolbar_notification_right_margin"
                    android:padding="@dimen/inc_toolbar_icon_padding"
                    android:src="@drawable/profile_delete_icon"
                    android:visibility="gone" />
            </RelativeLayout>
        </RelativeLayout>
    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

请稍微更新一下您的代码:

  1. 风格: <style name="AppTheme" parent="Theme.AppCompat.Light">

  2. XML 文件:

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
         <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 
</android.support.design.widget.AppBarLayout> 

LinearLayout 用作 parent ,child 布局可能隐藏了 toolBar

<LinearLayout 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"
tools:context="com.example.myproj.activities.RegisterActivity">

<include layout="@layout/toolbar"/>

  //other childs

</LinearLayout>

要将标题设置为工具栏,请使用 getSupportActionBar().setTitle(title);

 toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(title);

尝试删除自定义工具栏中的主题:

<?xml version="1.0" encoding="utf-8"?>
  <android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"  //REMOVE THIS
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    app:subtitleTextColor="@color/sub_text_color"
    app:navigationContentDescription="@string/abc_action_bar_up_description"
    android:background="@color/sub_text_color"
    app:navigationIcon="?attr/homeAsUpIndicator"
  />

我觉得这个主题很有影响....

造成这种情况的原因有很多。

首先,放置在自定义工具栏之后的视图可能会覆盖在其上,从而挡住了它的视线。请记住,您正在使用 RelativeLayout 作为根元素。因此,您需要确保工具栏之后的视图具有 android:layout_below="@+id/toolbar" 属性。

其次,我建议您使用协调器布局作为根视图,并将自定义工具栏包装在 AppBarLayout 视图中。这样您的布局文件类似于:

<android.support.design.widget.CoordinatorLayout 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"
tools:context="ng.shoppi.lafarge_app.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

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

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

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

这将使工具栏保持在顶部,并确保其他视图位于工具栏下方。 祝你好运。