我在哪里XML定义为工具栏小部件的Android 5.0? [英] Where do I define XML for the Toolbar widget in Android 5.0?
问题描述
好了,我已经经历几个StackOverflow的帖子了,但我仍然困惑,在这里该XML我的工具栏去。
Okay, I've been going through several StackOverflow posts now, but I'm still confused as to where this xml for my Toolbar goes.
<android.support.v7.widget.Toolbar
android:id="@+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="@styles/colorPrimary" />
这是否走在我的 /layout/activity_main.xml
?
推荐答案
工具栏的动作条的应用程序布局中使用的推广,有两种做法,现在回答你的问题:
Toolbar is a generalization of Action bars for use within app layouts, now to answer your question there are two practices:
不好的做法:
不好的做法是定义工具栏在每一个布局。
Bad practice is to define the Toolbar in every layouts.
标准做法:
标准的做法是定义布局和引用它在碱的活性。你只需要包括在任何布局您希望此工具栏布局(通过使用&LT;包括&GT;
)和扩展定义的基本活动在任何活动
Standard practice is to define a layout and reference it in a base activity. You just need to include this Toolbar layout in whichever layout you want (by using <include>
) and extend the defined base activity in whichever activity.
此标准的做法将帮助您保持对工具栏单code碱基,节省您的时间从每一次定义工具栏。
This standard practice will help you keeping a single code base for Toolbar and save your time from defining Toolbar every time.
示例:谷歌I / O 2014年Android应用程序
<一个href="https://github.com/google/iosched/blob/master/android/src/main/res/layout/toolbar_actionbar_with_headerbar.xml"相对=nofollow> toolbar_actionbar_with_headerbar.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:iosched="http://schemas.android.com/apk/res-auto"
style="@style/HeaderBar"
iosched:theme="@style/ActionBarThemeOverlay"
iosched:popupTheme="@style/ActionBarPopupThemeOverlay"
android:id="@+id/toolbar_actionbar"
iosched:titleTextAppearance="@style/ActionBar.TitleText"
iosched:contentInsetStart="?actionBarInsetStart"
android:layout_width="match_parent"
android:layout_height="?actionBarSize" />
此工具栏布局中引用设置活动,如下所示:
This toolbar layout is referenced in settings activity as given below:
<一个href="https://github.com/google/iosched/blob/master/android/src/main/res/layout/activity_settings.xml"相对=nofollow> activity_settings.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.SettingsActivity">
<include layout="@layout/toolbar_actionbar_with_headerbar" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
这篇关于我在哪里XML定义为工具栏小部件的Android 5.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!