在所有活动中使用工具栏(Android) [英] Use Toolbar across all activities (Android)

查看:73
本文介绍了在所有活动中使用工具栏(Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用工具栏替换ActionBar.一切顺利,只有一个问题:

I'm using a Toolbar to replace the ActionBar. All is going well with one problem:

工具栏仅显示在主要活动上.

The toolbar shows only on the main activity.

如果我尝试以与主活动相同的方式调用任何活动上的工具栏,则当我调用该活动时,应用将崩溃.

If I try call the toolbar on any activity the same way I did with the main activity the app will crash when I call that activity.

如果我尝试为CreateOptionsMenu上的工具栏充气,当我调用该活动时,该活动将崩溃.

If I try to inflate the toolbar onCreateOptionsMenu, that activity will crash when I call it.

如何在所有活动中而不是主要活动中调用/使用相同的工具栏.

How can I call/use the same toolbar across all my activities and not just the main one.

下面是一些代码:

public android.support.v7.widget.Toolbar toolbar;

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about);
    toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.app_bar_id);
    setSupportActionBar(toolbar);
}

上面的代码可以成功调用工具栏,但是仅当我在主要活动上使用它时,该代码才有效,如果我在上面所示的相同方法中调用工具栏,则其余活动将崩溃.

The code above works to call the toolbar successfully, but it only works if i use it on the main activity, the rest of the activities will crash if I called the toolbar there the same method shown above.

请帮忙吗?

谢谢.

根据请求,这里有更多代码片段:

Upon Request here are more code fragments:

app_bar.xml:

app_bar.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:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/actionbarbgcolor"
app:popupTheme="@style/popUpTheme">

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

themes.xml(替换styles.xml):

themes.xml (styles.xml replacement):

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">

<style name="DefaultActionBarTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:textColorPrimary">@color/windowbackgroundcolor</item>
    <item name="android:windowBackground">@color/windowbackgroundcolor</item>
</style>

<style name="popUpTheme">
    <item name="android:textColor">@color/actionbarbgcolor</item>
</style>
</resources>

推荐答案

我找到了解决方案,但我忘记将工具栏包含在其余的活动布局文件中.因此,我正在调用该活动的布局中不存在的工具栏.

I found the solution, I forgot to include the toolbar in the rest of the activities layout files. So I was calling a toolbar that didn't exist in that activity's layout.

我只将它包含在主要活动中,所以这就是为什么它在那里工作并在其余活动时崩溃的原因.

I only had it included in the main activity so that's why it worked there and crashed at the rest.

对于初学者来说,这意味着您希望工具栏在其中使用的每个布局xml文件中都必须存在以下代码:

For beginners, this mean the following code must exist in every layout xml file you wish the toolbar to work in:

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

请注意,"app_bar"只是我用工具栏命名的名称,您的可以不同.

Note that "app_bar" is just the name I called my toolbar with, yours can be different.

祝你好运.

这篇关于在所有活动中使用工具栏(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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