我必须为Android应用程序中的每个活动创建一个工具栏吗?
我对Android中的新工具栏有疑问.
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 activities?
我尝试创建一个对布局进行填充的Singleton,并搜索一个视图ID以创建一个工具栏,并为所有活动返回相同的实例,但这是行不通的.
I try create a Singleton inflating a layout and search a view ID to create a toolbar and return the same instance for all activities, but this don't work.
有人可以帮助我吗?:S
Can anybody help me? :S
工具栏
只是一个视图,您必须将其添加到所需的每个 Activity
中显示它.
Toolbar
is just a view and you have to add it to your each Activity
in which you want to show it.
一种方法是将其放在单独的布局文件中,然后将其包含在您的 Activity
布局中.
One way is to just put it in a separate layout file and include in your Activity
layout.
toolbar.xml
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"/>