7

好的,我現在經歷了幾個StackOverflow帖子,但我仍然對我的工具欄的這個xml的位置感到困惑。我在哪裏可以定義Android 5.0中工具欄小部件的XML?

<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

回答

21

工具欄的動作條的應用程序佈局中使用的推廣,現在回答你的問題有兩種做法:

錯誤做法:

壞的做法是在每個佈局定義工具欄。

標準做法:

標準的做法是定義佈局和在鹼活性引用它。您只需將此工具欄佈局包含在您想要的任何佈局中(通過使用<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> 
+2

有用和非常豐富的答案。感謝幫助我理解這一點! – wasimsandhu 2014-11-04 02:16:48

+1

@awkwardgiraffe歡迎和歡呼! Google I/O是探索新風格,實施指南和代碼的最佳開源應用程序。 – 2014-11-04 05:45:16

+0

@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

1

至於我,我通常做一個ToolbarActivity。接下來,如果你想要你的活動有一個工具欄,你只需要YourActivity extends ToolbarActivity

public class ToolbarActivity extends AppCompatActivity { 

    @Override 
    public void setContentView(int layoutResID) { 
     super.setContentView(R.layout.activity_toolbar); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     LayoutInflater inflater = LayoutInflater.from(this); 
     View contentView = inflater.inflate(layoutResID, null); 

     LinearLayout layout = (LinearLayout) findViewById(R.id.layout); 
     layout.addView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
    } 
} 

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" 
    android:id="@+id/layout" 
    tools:context=".ToolbarActivity" > 

    <android.support.v7.widget.Toolbar 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:minHeight="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     android:id="@+id/toolbar" /> 

</LinearLayout> 
相關問題