简体   繁体   English

自定义工具栏不存在

[英]Custom ToolBar not there

I have decided to make my own Tool bar 我决定制作自己的工具栏

So i removed the regular ToolBar : 所以我删除了常规工具栏:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="false"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar> //removetollbar

and make my own tool bar 并制作自己的工具栏

<?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.support.v7.widget.Toolbar>

then i include in main xml 然后我包含在主xml中

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

</include>

and in the code i set it up : 并在代码中将其设置为:

_CustomToolBar = (android.support.v7.widget.Toolbar) 
findViewById(R.id.custombar);
setSupportActionBar(_CustomToolBar);
android.support.v7.widget.Toolbar _CustomToolBar;

now when the app run the custom toolbar is not there 现在当应用程序运行自定义工具栏时不存在

Please help. 请帮忙。

In your <include> tag, you've set the id of the toolbar to be @+id/custombar , but you're looking it up using R.id.toolbar . <include>标记中,您已将工具栏的ID设置为@+id/custombar ,但您正在使用R.id.toolbar进行R.id.toolbar Change that line to this: 将该行更改为此:

_CustomToolBar = (android.support.v7.widget.Toolbar) findViewById(R.id.custombar);

Didn't get your question clearly, but still I will post how I code to set custom toolbar. 不清楚您的问题,但我仍然会发布如何设置自定义工具栏的代码。

1) My Manifest have NoActionBar theme to remove default toolbar. 1)我的清单具有NoActionBar主题以删除默认工具栏。

2) My custom_toolbar.xml file - 2)我的custom_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"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="5dp"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

3) Way I include it in my main.xml 3)我将其包含在main.xml中的方式

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

4) My Java code - 4)我的Java代码-

// In onCreate method
android.support.v7.widget.Toolbar _CustomToolBar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(_CustomToolBar);

And it is working properly. 而且它工作正常。

在此处输入图片说明

So Here whats the different between our code. 因此,我们的代码之间有什么不同。

1) Give your custom toolbar proper id android:id="@+id/customToolbar" instead of giving it to you include . 1)给您的自定义工具栏正确的id android:id="@+id/customToolbar"而不android:id="@+id/customToolbar"include

2) Give height and background color to your custom toolbar so that it will be visible properly 2)为自定义工具栏设置高度和背景色,以便正确显示

android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"

3) And instead of linking include id to toolbar, link your custom toolbar id 3)而不是将include id链接到工具栏,而是链接custom toolbar id

findViewById(R.id.customToolbar);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM