工具欄的動作條的應用程序佈局中使用的推廣,現在回答你的問題有兩種做法:
錯誤做法:
壞的做法是在每個佈局定義工具欄。
標準做法:
標準的做法是定義佈局和在鹼活性引用它。您只需將此工具欄佈局包含在您想要的任何佈局中(通過使用<include>
),並將所定義的基本活動擴展到任何活動中。
本標準實踐將幫助您保持工具欄的單一代碼庫,並節省您每次定義工具欄的時間。
例子:谷歌I/O 2014 Android應用
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" />
下面給出此工具欄佈局在設置活動中引用:
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>
有用和非常豐富的答案。感謝幫助我理解這一點! – wasimsandhu 2014-11-04 02:16:48
@awkwardgiraffe歡迎和歡呼! Google I/O是探索新風格,實施指南和代碼的最佳開源應用程序。 – 2014-11-04 05:45:16
@PareshMayani你能幫我嗎? http://stackoverflow.com/questions/26903014/android-adt-23-0-4-is-showing-compile-with-api21android-4-xl-preview-instead – 2014-11-13 07:14:01