设置Android工具栏会导致程序失败

时间:2015-11-16 11:01:03

标签: android android-layout android-toolbar

我在layout文件中声明了一个工具栏。

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

我将其设置为我的活动中使用的工具栏。

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

但是当我运行它时,抛出了以下异常。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dynamicwebapac.commerce.mobile/com.dynamicwebapac.commerce.mobile.activities.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
            at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:199)
            at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
            at com.dynamicwebapac.commerce.mobile.activities.MainActivity.onCreate(MainActivity.java:184)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我正在重复使用其他程序的代码,但我不知道该怎么回事。我确实尝试过设置

<item name="windowActionBar">false</item>
style文件中的

但这样做也会导致程序的其他部分失败。有谁知道如何阻止这个问题的产生?

样式文件

<style name="CardView">
        <item name="cardBackgroundColor">@color/cardview_light_background</item>
        <item name="cardCornerRadius">@dimen/cardview_default_radius</item>
        <item name="cardElevation">@dimen/cardview_default_elevation</item>
        <item name="cardMaxElevation">@dimen/cardview_default_elevation</item>
        <item name="cardUseCompatPadding">false</item>
        <item name="cardPreventCornerOverlap">true</item>
    </style>
    <style name="CardView.Light" parent="@style/CardView">
        <item name="cardBackgroundColor">@color/cardview_light_background</item>
    </style>
    <style name="CardView.Dark" parent="@style/CardView">
        <item name="cardBackgroundColor">@color/cardview_dark_background</item>
    </style>
    <style name="Widget.AppCompat.ActionBar" parent="@style/Base.Widget.AppCompat.ActionBar"/>
    <style name="Widget.AppCompat.Light.ActionBar" parent="@style/Base.Widget.AppCompat.Light.ActionBar"/>
    <style name="Widget.AppCompat.ActionBar.Solid" parent="@style/Base.Widget.AppCompat.ActionBar.Solid"/>
    <style name="Widget.AppCompat.Light.ActionBar.Solid" parent="@style/Base.Widget.AppCompat.Light.ActionBar.Solid"/>
    <style name="TextAppearance.AppCompat.Widget.ActionBar.Title"
           parent="@style/Base.TextAppearance.AppCompat.Widget.ActionBar.Title"/>
    <style name="TextAppearance.AppCompat.Widget.ActionBar.Subtitle"
           parent="@style/Base.TextAppearance.AppCompat.Widget.ActionBar.Subtitle"/>
    <style name="TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
           parent="@style/Base.TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"/>
    <style name="TextAppearance.AppCompat.Widget.ActionBar.Subtitle.Inverse"
           parent="@style/Base.TextAppearance.AppCompat.Widget.ActionBar.Subtitle.Inverse"/>
    <style name="Widget.AppCompat.ProgressBar.Horizontal" parent="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"/>
    <style name="Widget.AppCompat.ProgressBar" parent="@style/Base.Widget.AppCompat.ProgressBar"/>
    <style name="Widget.AppCompat.ActionButton" parent="@style/Base.Widget.AppCompat.ActionButton"/>
    <style name="Widget.AppCompat.ActionButton.CloseMode"
           parent="@style/RtlOverlay.Widget.AppCompat.ActionButton.CloseMode"/>
    <style name="Widget.AppCompat.ActionButton.Overflow"
           parent="@style/RtlOverlay.Widget.AppCompat.ActionButton.Overflow"/>
    <style name="Widget.AppCompat.ActionBar.TabBar" parent="@style/Base.Widget.AppCompat.ActionBar.TabBar"/>
    <style name="Widget.AppCompat.Light.ActionBar.TabBar" parent="@style/Base.Widget.AppCompat.Light.ActionBar.TabBar"/>
    <style name="Widget.AppCompat.ActionBar.TabView" parent="@style/Base.Widget.AppCompat.ActionBar.TabView"/>
    <style name="Widget.AppCompat.Light.ActionBar.TabView"
           parent="@style/Base.Widget.AppCompat.Light.ActionBar.TabView"/>
    <style name="Widget.AppCompat.ActionBar.TabText" parent="@style/Base.Widget.AppCompat.ActionBar.TabText"/>
    <style name="Widget.AppCompat.DrawerArrowToggle" parent="@style/Base.Widget.AppCompat.DrawerArrowToggle">
        <item name="color">?colorControlNormal</item>
    </style>
    <style name="Widget.AppCompat.Light.ActionBar.TabText"
           parent="@style/Base.Widget.AppCompat.Light.ActionBar.TabText"/>
    <style name="Widget.AppCompat.Light.ActionBar.TabText.Inverse"
           parent="@style/Base.Widget.AppCompat.Light.ActionBar.TabText.Inverse"/>
    <style name="TextAppearance.AppCompat.Widget.ActionBar.Menu"
           parent="@style/Base.TextAppearance.AppCompat.Widget.ActionBar.Menu"/>
    <style name="Widget.AppCompat.ActionMode" parent="@style/Base.Widget.AppCompat.ActionMode"/>
    <style name="TextAppearance.AppCompat.Widget.ActionMode.Title"
           parent="@style/Base.TextAppearance.AppCompat.Widget.ActionMode.Title"/>
    <style name="TextAppearance.AppCompat.Widget.ActionMode.Subtitle"
           parent="@style/Base.TextAppearance.AppCompat.Widget.ActionMode.Subtitle"/>
    <style name="TextAppearance.AppCompat.Widget.DropDownItem"
           parent="@style/Base.TextAppearance.AppCompat.Widget.DropDownItem"/>
    <style name="Widget.AppCompat.Spinner" parent="@style/Base.Widget.AppCompat.Spinner"/>
    <style name="Widget.AppCompat.Spinner.DropDown" parent="@style/Widget.AppCompat.Spinner"/>
    <style name="Widget.AppCompat.Spinner.DropDown.ActionBar"
           parent="@style/Base.Widget.AppCompat.Spinner.DropDown.ActionBar"/>
    <style name="Widget.AppCompat.DropDownItem.Spinner"
           parent="@style/RtlOverlay.Widget.AppCompat.Search.DropDown.Text"/>
    <style name="Widget.AppCompat.ListView.DropDown" parent="@style/Base.Widget.AppCompat.ListView.DropDown"/>
    <style name="TextAppearance.Widget.AppCompat.ExpandedMenu.Item"
           parent="@style/Base.TextAppearance.Widget.AppCompat.ExpandedMenu.Item"/>
    <style name="Widget.AppCompat.ListPopupWindow" parent="@style/Base.Widget.AppCompat.ListPopupWindow"/>
    <style name="Widget.AppCompat.PopupMenu.Overflow" parent="@style/Base.Widget.AppCompat.PopupMenu.Overflow"/>
    <style name="Widget.AppCompat.Light.PopupMenu.Overflow"
           parent="@style/Base.Widget.AppCompat.Light.PopupMenu.Overflow"/>
    <style name="Widget.AppCompat.PopupMenu" parent="@style/Base.Widget.AppCompat.PopupMenu"/>
    <style name="Widget.AppCompat.Light.PopupMenu" parent="@style/Base.Widget.AppCompat.Light.PopupMenu"/>
    <style name="Widget.AppCompat.ListView.Menu" parent="@style/Base.Widget.AppCompat.ListView.Menu"/>
    <style name="Widget.AppCompat.PopupWindow" parent="@style/Base.Widget.AppCompat.PopupWindow"/>
    <style name="TextAppearance.AppCompat.Widget.PopupMenu.Large"
           parent="@style/Base.TextAppearance.AppCompat.Widget.PopupMenu.Large"/>
    <style name="TextAppearance.AppCompat.Widget.PopupMenu.Small"
           parent="@style/Base.TextAppearance.AppCompat.Widget.PopupMenu.Small"/>
    <style name="TextAppearance.AppCompat.Light.Widget.PopupMenu.Large"
           parent="@style/Base.TextAppearance.AppCompat.Light.Widget.PopupMenu.Large"/>
    <style name="TextAppearance.AppCompat.Light.Widget.PopupMenu.Small"
           parent="@style/Base.TextAppearance.AppCompat.Light.Widget.PopupMenu.Small"/>
    <style name="TextAppearance.AppCompat.SearchResult.Title"
           parent="@style/Base.TextAppearance.AppCompat.SearchResult.Title"/>
    <style name="TextAppearance.AppCompat.SearchResult.Subtitle"
           parent="@style/Base.TextAppearance.AppCompat.SearchResult.Subtitle"/>
    <style name="Widget.AppCompat.AutoCompleteTextView" parent="@style/Base.Widget.AppCompat.AutoCompleteTextView"/>
    <style name="Widget.AppCompat.Light.AutoCompleteTextView"
           parent="@style/Base.Widget.AppCompat.Light.AutoCompleteTextView"/>
    <style name="Widget.AppCompat.ActivityChooserView" parent="@style/Base.Widget.AppCompat.ActivityChooserView"/>
    <style name="Widget.AppCompat.Light.ActivityChooserView"
           parent="@style/Base.Widget.AppCompat.Light.ActivityChooserView"/>
    <style name="Widget.AppCompat.SearchView" parent="@style/Base.Widget.AppCompat.SearchView"/>
    <style name="Widget.AppCompat.EditText" parent="@style/Base.Widget.AppCompat.EditText"/>
    <style name="Widget.AppCompat.CompoundButton.Switch" parent="@style/Base.Widget.AppCompat.CompoundButton.Switch"/>
    <style name="Widget.AppCompat.Toolbar" parent="@style/Base.Widget.AppCompat.Toolbar"/>
    <style name="Widget.AppCompat.Toolbar.Button.Navigation"
           parent="@style/Base.Widget.AppCompat.Toolbar.Button.Navigation"/>
    <style name="TextAppearance.Widget.AppCompat.Toolbar.Title"
           parent="@style/Base.TextAppearance.Widget.AppCompat.Toolbar.Title"/>
    <style name="TextAppearance.Widget.AppCompat.Toolbar.Subtitle"
           parent="@style/Base.TextAppearance.Widget.AppCompat.Toolbar.Subtitle"/>
    <style name="TextAppearance.AppCompat" parent="@style/Base.TextAppearance.AppCompat"/>
    <style name="TextAppearance.AppCompat.Display4" parent="@style/Base.TextAppearance.AppCompat.Display4"/>
    <style name="TextAppearance.AppCompat.Display3" parent="@style/Base.TextAppearance.AppCompat.Display3"/>
    <style name="TextAppearance.AppCompat.Display2" parent="@style/Base.TextAppearance.AppCompat.Display2"/>
    <style name="TextAppearance.AppCompat.Display1" parent="@style/Base.TextAppearance.AppCompat.Display1"/>
    <style name="TextAppearance.AppCompat.Headline" parent="@style/Base.TextAppearance.AppCompat.Headline"/>
    <style name="TextAppearance.AppCompat.Title" parent="@style/Base.TextAppearance.AppCompat.Title"/>
    <style name="TextAppearance.AppCompat.Title.Inverse" parent="@style/Base.TextAppearance.AppCompat.Title.Inverse"/>
    <style name="TextAppearance.AppCompat.Subhead" parent="@style/Base.TextAppearance.AppCompat.Subhead"/>
    <style name="TextAppearance.AppCompat.Subhead.Inverse"
           parent="@style/Base.TextAppearance.AppCompat.Subhead.Inverse"/>
    <style name="TextAppearance.AppCompat.Body2" parent="@style/Base.TextAppearance.AppCompat.Body2"/>
    <style name="TextAppearance.AppCompat.Body1" parent="@style/Base.TextAppearance.AppCompat.Body1"/>
    <style name="TextAppearance.AppCompat.Caption" parent="@style/Base.TextAppearance.AppCompat.Caption"/>
    <style name="TextAppearance.AppCompat.Menu" parent="@style/Base.TextAppearance.AppCompat.Menu"/>
    <style name="TextAppearance.AppCompat.Inverse" parent="@style/Base.TextAppearance.AppCompat.Inverse"/>
    <style name="TextAppearance.AppCompat.Large" parent="@style/Base.TextAppearance.AppCompat.Large"/>
    <style name="TextAppearance.AppCompat.Large.Inverse" parent="@style/Base.TextAppearance.AppCompat.Large.Inverse"/>
    <style name="TextAppearance.AppCompat.Medium" parent="@style/Base.TextAppearance.AppCompat.Medium"/>
    <style name="TextAppearance.AppCompat.Medium.Inverse" parent="@style/Base.TextAppearance.AppCompat.Medium.Inverse"/>
    <style name="TextAppearance.AppCompat.Small" parent="@style/Base.TextAppearance.AppCompat.Small"/>
    <style name="TextAppearance.AppCompat.Small.Inverse" parent="@style/Base.TextAppearance.AppCompat.Small.Inverse"/>
    <style name="TextAppearance.AppCompat.Button" parent="@style/Base.TextAppearance.AppCompat.Button"/>
    <style name="TextAppearance.AppCompat.Widget.Switch" parent="@style/Base.TextAppearance.AppCompat.Widget.Switch"/>
    <style name="Widget.AppCompat.Light.ActionBar.Solid.Inverse"
           parent="@style/Widget.AppCompat.Light.ActionBar.Solid"/>
    <style name="Widget.AppCompat.Light.ActionBar.TabBar.Inverse"
           parent="@style/Widget.AppCompat.Light.ActionBar.TabBar"/>
    <style name="Widget.AppCompat.Light.ActionBar.TabView.Inverse"
           parent="@style/Widget.AppCompat.Light.ActionBar.TabView"/>
    <style name="TextAppearance.AppCompat.Light.SearchResult.Title"
           parent="@style/TextAppearance.AppCompat.SearchResult.Title"/>
    <style name="TextAppearance.AppCompat.Light.SearchResult.Subtitle"
           parent="@style/TextAppearance.AppCompat.SearchResult.Subtitle"/>
    <style name="Widget.AppCompat.Light.ActionMode.Inverse" parent="@style/Widget.AppCompat.ActionMode"/>
    <style name="TextAppearance.AppCompat.Widget.ActionMode.Title.Inverse"
           parent="@style/TextAppearance.AppCompat.Widget.ActionMode.Title"/>
    <style name="TextAppearance.AppCompat.Widget.ActionMode.Subtitle.Inverse"
           parent="@style/TextAppearance.AppCompat.Widget.ActionMode.Subtitle"/>
    <style name="Widget.AppCompat.Light.DropDownItem.Spinner" parent="@style/Widget.AppCompat.DropDownItem.Spinner"/>
    <style name="Widget.AppCompat.Light.SearchView" parent="@style/Widget.AppCompat.SearchView"/>
    <style name="Widget.AppCompat.Light.ActionButton" parent="@style/Widget.AppCompat.ActionButton"/>
    <style name="Widget.AppCompat.Light.ActionButton.Overflow" parent="@style/Widget.AppCompat.ActionButton.Overflow"/>
    <style name="Widget.AppCompat.Light.ActionButton.CloseMode"
           parent="@style/Widget.AppCompat.ActionButton.CloseMode"/>
    <style name="Widget.AppCompat.Light.Spinner.DropDown.ActionBar"
           parent="@style/Widget.AppCompat.Spinner.DropDown.ActionBar"/>
    <style name="Widget.AppCompat.Light.ListView.DropDown" parent="@style/Widget.AppCompat.ListView.DropDown"/>
    <style name="Widget.AppCompat.Light.ListPopupWindow" parent="@style/Widget.AppCompat.ListPopupWindow"/>
    <style name="Base.Widget.AppCompat.ActionBar" parent="">
        <item name="android:gravity">center_vertical</item>
        <item name="height">?actionBarSize</item>
        <item name="actionOverflowButtonStyle">@style/Widget.AppCompat.ActionButton.Overflow</item>
        <item name="actionButtonStyle">@style/Widget.AppCompat.ActionButton</item>
        <item name="displayOptions">showTitle</item>
        <item name="titleTextStyle">@style/TextAppearance.AppCompat.Widget.ActionBar.Title</item>
        <item name="subtitleTextStyle">@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle</item>
        <item name="divider">?dividerVertical</item>
        <item name="background">@null</item>
        <item name="backgroundStacked">@null</item>
        <item name="backgroundSplit">@null</item>
        <item name="elevation">8.0dip</item>
        <item name="popupTheme">?actionBarPopupTheme</item>
    </style>
    <style name="Base.Widget.AppCompat.Light.ActionBar" parent="@style/Base.Widget.AppCompat.ActionBar">
        <item name="actionOverflowButtonStyle">@style/Widget.AppCompat.Light.ActionButton.Overflow</item>
        <item name="actionButtonStyle">@style/Widget.AppCompat.Light.ActionButton</item>
    </style>
    <style name="Base.Widget.AppCompat.ActionBar.Solid" parent="@style/Base.Widget.AppCompat.ActionBar">
        <item name="background">?colorPrimary</item>
        <item name="backgroundStacked">?colorPrimary</item>
        <item name="backgroundSplit">?colorPrimary</item>
    </style>
    <style name="Base.Widget.AppCompat.Light.ActionBar.Solid" parent="@style/Base.Widget.AppCompat.Light.ActionBar">
        <item name="background">?colorPrimary</item>
        <item name="backgroundStacked">?colorPrimary</item>
        <item name="backgroundSplit">?colorPrimary</item>
    </style>
    <style name="Base.Widget.AppCompat.ActionButton" parent="">
        <item name="android:gravity">center</item>
        <item name="android:background">?actionBarItemBackground</item>
        <item name="android:paddingLeft">12.0dip</item>
        <item name="android:paddingRight">12.0dip</item>
        <item name="android:scaleType">center</item>
        <item name="android:minWidth">@dimen/abc_action_button_min_width_material</item>
        <item name="android:minHeight">@dimen/abc_action_button_min_height_material</item>
        <item name="android:maxLines">2</item>
        <item name="textAllCaps">@bool/abc_config_actionMenuItemAllCaps</item>
    </style>
    <style name="Base.Widget.AppCompat.ActionButton.CloseMode" parent="@style/Base.Widget.AppCompat.ActionButton">
        <item name="android:background">?selectableItemBackgroundBorderless</item>
    </style>
    <style name="Base.Widget.AppCompat.ActionButton.Overflow" parent="@style/Base.Widget.AppCompat.ActionButton">
        <item name="android:background">?actionBarItemBackground</item>
        <item name="android:src">@drawable/abc_ic_menu_moreoverflow_mtrl_alpha</item>
        <item name="android:minWidth">@dimen/abc_action_button_min_width_overflow_material</item>
        <item name="android:minHeight">@dimen/abc_action_button_min_height_material</item>
        <item name="android:contentDescription">@string/abc_action_menu_overflow_description</item>
    </style>
    <style name="Base.Widget.AppCompat.ActionBar.TabBar" parent="">
        <item name="divider">?actionBarDivider</item>
        <item name="showDividers">middle</item>
        <item name="dividerPadding">8.0dip</item>
    </style>
    <style name="Base.Widget.AppCompat.Light.ActionBar.TabBar" parent="@style/Base.Widget.AppCompat.ActionBar.TabBar"/>
    <style name="Base.Widget.AppCompat.ActionBar.TabView" parent="">
        <item name="android:gravity">center_horizontal</item>
        <item name="android:background">@drawable/abc_tab_indicator_material</item>
        <item name="android:paddingLeft">16.0dip</item>
        <item name="android:paddingRight">16.0dip</item>
        <item name="android:layout_width">0.0dip</item>
        <item name="android:minWidth">80.0dip</item>
        <item name="android:layout_weight">1.0</item>
    </style>
    <style name="Base.Widget.AppCompat.Light.ActionBar.TabView" parent="@style/Base.Widget.AppCompat.ActionBar.TabView">
        <item name="android:background">@drawable/abc_tab_indicator_material</item>
    </style>
    <style name="Base.Widget.AppCompat.ActionBar.TabText" parent="">
        <item name="android:textAppearance">@style/TextAppearance.AppCompat.Medium</item>
        <item name="android:textSize">12.0sp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">?android:textColorPrimary</item>
        <item name="android:ellipsize">marquee</item>
        <item name="android:maxWidth">180.0dip</item>
        <item name="android:maxLines">2</item>
        <item name="textAllCaps">true</item>
    </style>
    <style name="Base.Widget.AppCompat.Light.ActionBar.TabText"
           parent="@style/Base.Widget.AppCompat.ActionBar.TabText"/>
    <style name="Base.Widget.AppCompat.Light.ActionBar.TabText.Inverse"
           parent="@style/Base.Widget.AppCompat.Light.ActionBar.TabText">
        <item name="android:textAppearance">@style/TextAppearance.AppCompat.Medium.Inverse</item>
    </style>
    <style name="Base.Widget.AppCompat.ActionMode" parent="">
        <item name="height">?actionBarSize</item>
        <item name="titleTextStyle">@style/TextAppearance.AppCompat.Widget.ActionMode.Title</item>
        <item name="subtitleTextStyle">@style/TextAppearance.AppCompat.Widget.ActionMode.Subtitle</item>
        <item name="background">?actionModeBackground</item>
        <item name="backgroundSplit">?actionModeSplitBackground</item>
        <item name="closeItemLayout">@layout/abc_action_mode_close_item_material</item>
    </style>
    <style name="Base.TextAppearance.AppCompat.Widget.ActionMode.Title" parent="@style/TextAppearance.AppCompat.Title">
        <item name="android:textSize">@dimen/abc_text_size_title_material_toolbar</item>
    </style>
    <style name="Base.TextAppearance.AppCompat.Widget.ActionMode.Subtitle"
           parent="@style/TextAppearance.AppCompat.Subhead">
        <item name="android:textSize">@dimen/abc_text_size_subtitle_material_toolbar</item>
    </style>
    <style name="Base.TextAppearance.AppCompat.Widget.ActionBar.Menu" parent="@android:style/TextAppearance.Small">
        <item name="android:textSize">12.0sp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">?actionMenuTextColor</item>
    </style>
    <style name="Base.TextAppearance.AppCompat.Widget.ActionBar.Title" parent="@style/TextAppearance.AppCompat.Title">
        <item name="android:textSize">@dimen/abc_text_size_title_material_toolbar</item>
    </style>
    <style name="Base.TextAppearance.AppCompat.Widget.ActionBar.Subtitle"
           parent="@style/TextAppearance.AppCompat.Subhead">
        <item name="android:textSize">@dimen/abc_text_size_subtitle_material_toolbar</item>
    </style>
    <style name="Base.TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
           parent="@style/TextAppearance.AppCompat.Title.Inverse">
        <item name="android:textSize">@dimen/abc_text_size_title_material_toolbar</item>
    </style>
    <style name="Base.TextAppearance.AppCompat.Widget.ActionBar.Subtitle.Inverse"
           parent="@style/TextAppearance.AppCompat.Subhead.Inverse">
        <item name="android:textSize">@dimen/abc_text_size_subtitle_material_toolbar</item>
    </style>
    <style name="Base.Widget.AppCompat.ProgressBar.Horizontal" parent="@android:style/Widget.ProgressBar.Horizontal"/>
    <style name="Base.Widget.AppCompat.ProgressBar" parent="@android:style/Widget.ProgressBar">
        <item name="android:maxWidth">@dimen/abc_action_bar_progress_bar_size</item>
        <item name="android:maxHeight">@dimen/abc_action_bar_progress_bar_size</item>
        <item name="android:minWidth">@dimen/abc_action_bar_progress_bar_size</item>
        <item name="android:minHeight">@dimen/abc_action_bar_progress_bar_size</item>
    </style>
    <style name="Base.Widget.AppCompat.Spinner" parent="@android:style/Widget.Spinner">
        <item name="android:background">@drawable/abc_spinner_mtrl_am_alpha</item>
        <item name="android:dropDownSelector">?listChoiceBackgroundIndicator</item>
        <item name="android:dropDownVerticalOffset">0.0dip</item>
    </style>
    <style name="Base.Widget.AppCompat.Spinner.DropDown.ActionBar" parent="@android:style/Widget">
        <item name="android:gravity">start|center</item>
        <item name="android:background">@drawable/abc_spinner_mtrl_am_alpha</item>
        <item name="android:clickable">true</item>
        <item name="android:dropDownSelector">?listChoiceBackgroundIndicator</item>
        <item name="android:popupBackground">@drawable/abc_popup_background_mtrl_mult</item>
        <item name="android:dropDownWidth">wrap_content</item>
        <item name="android:dropDownHorizontalOffset">0.0dip</item>
        <item name="android:dropDownVerticalOffset">0.0dip</item>
        <item name="overlapAnchor">true</item>
    </style>
    <style name="Base.Widget.AppCompat.DropDownItem.Spinner" parent="">
        <item name="android:textAppearance">@style/TextAppearance.AppCompat.Widget.DropDownItem</item>
        <item name="android:gravity">center_vertical</item>
        <item name="android:paddingLeft">8.0dip</item>
        <item name="android:paddingRight">8.0dip</item>
    </style>
    <style name="Base.Widget.AppCompat.ListView.DropDown" parent="@android:style/Widget.ListView">
        <item name="android:listSelector">?listChoiceBackgroundIndicator</item>
        <item name="android:divider">@null</item>
    </style>
    <style name="Base.TextAppearance.AppCompat.Widget.DropDownItem" parent="@android:style/TextAppearance.Small">
        <item name="android:textColor">?android:textColorPrimaryDisableOnly</item>
    </style>
    <style name="Base.TextAppearance.Widget.AppCompat.ExpandedMenu.Item" parent="@android:style/TextAppearance.Medium">
        <item name="android:textColor">?android:textColorPrimaryDisableOnly</item>
    </style>
    <style name="Base.Widget.AppCompat.ListView.Menu" parent="@android:style/Widget.ListView.Menu">
        <item name="android:listSelector">?listChoiceBackgroundIndicator</item>
        <item name="android:divider">?dividerHorizontal</item>
    </style>


    ...

2 个答案:

答案 0 :(得分:1)

我们需要从主题中删除旧的操作栏。打开res-&gt; values-&gt; styles.xml文件并添加以下代码行。我们选择了不提供操作栏的Theme.AppCompat.Light.NoActionBar主题。

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>


</resources>

您可以在下面找到实现Android操作工具栏的活动的完整代码。您可以在下面找到activity_main.xml文件的代码。

<RelativeLayout      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"
tools:context="com.techlovejump.materialdesign.MainActivity">

<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="#ff6d7fe2"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
></android.support.v7.widget.Toolbar>

<TextView Android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"/>


</RelativeLayout>

您的MainActivity类包含以下内容:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}
}

答案 1 :(得分:0)

我使用此代码在我的活动中设置工具栏。  在java类中

private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

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

        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
}

在xml文件中创建一个单独的toolbar.xml for toolbar

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:gravity="center"
    android:minHeight="?attr/actionBarSize"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >

    <TextView
        android:id="@+id/edit_text_heder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/size10"

        android:textColor="#fff"
         android:gravity="center"
        android:textSize="20sp" />

</android.support.v7.widget.Toolbar>

然后在activity_login.xml

<include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />