Use Toolbar across all activities (Android)

懵懂的女人 提交于 2019-12-03 04:46:32

问题


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.

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.

here is some pieces of the code:

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.

Some help please?

Thank you.

Edited:

Upon Request here are more code fragments:

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

回答1:


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.

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"/>

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

Good luck.




回答2:


you can create a layout with follow code that contain toolbar

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
    android:fitsSystemWindows="true"
    tools:context="com.tejariapp.myapplicationtester1.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>

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

pay attention to height value ,if you don't set it wrap_content value you can see that other elements gone below toolbar and then in each layout that you want to have toolbar ,simply you can write this

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


来源:https://stackoverflow.com/questions/28661981/use-toolbar-across-all-activities-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!