I have to create one Toolbar for each activity in my Android app?

前端 未结 3 2098
我在风中等你
我在风中等你 2021-02-14 19:35

I have this doubt about new Toolbar in Android.

I have to create one Toolbar for each activity in my app or there are a best practice to create one Toolbar for all activ

相关标签:
3条回答
  • 2021-02-14 19:58

    You can also extend Your activity class with AppCompatActivity instead of Activity

    0 讨论(0)
  • 2021-02-14 20:02

    Toolbar is just a view and you have to add it to your each Activity in which you want to show it.

    One way is to just put it in a separate layout file and include in your Activity layout.

    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="wrap_content"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >
    
    </android.support.v7.widget.Toolbar>
    

    Now in your Activity layout where you want to add it just include like this:

    <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar"/>
    
    0 讨论(0)
  • 2021-02-14 20:10
    <?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="wrap_content"
    android:background="?attr/colorPrimary"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    >
    
    </android.support.v7.widget.Toolbar>
    
    0 讨论(0)
提交回复
热议问题