【问题标题】:ActionBar styling after updating to Android lollipop更新到 Android lollipop 后的 ActionBar 样式
【发布时间】:2014-10-21 08:03:16
【问题描述】:

我昨天通过选择 API19 开发自定义 ActionBar,今天我将支持库和项目更新到 API21,导致我遇到问题。在我的操作栏中,有一个我不需要的菜单到箭头按钮,并且操作栏的样式发生了变化。

更新前:

更新后:

下面是style.xml

<resources>
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">

    </style>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:buttonStyle">@style/ButtonAppTheme</item>

        <item name="actionBarItemBackground">@drawable/selectable_background_apptheme</item>
        <item name="popupMenuStyle">@style/PopupMenu.Apptheme</item>
        <item name="dropDownListViewStyle">@style/DropDownListView.Apptheme</item>
        <item name="actionBarTabStyle">@style/ActionBarTabStyle.Apptheme</item>
        <item name="actionDropDownStyle">@style/DropDownNav.Apptheme</item>
        <item name="actionBarStyle">@style/ActionBar.Solid.Apptheme</item>
        <item name="actionModeBackground">@drawable/cab_background_top_apptheme</item>
        <item name="actionModeSplitBackground">@drawable/cab_background_bottom_apptheme</item>
        <item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Apptheme</item>

        <!-- Light.DarkActionBar specific -->
        <item name="actionBarWidgetTheme">@style/Theme.Apptheme.Widget</item>

    </style>

    <!-- Base application theme for Full screen Activities -->
    <style name="AppThemeFullScreen" parent="@android:style/Theme.Light.NoTitleBar.Fullscreen">
        <item name="android:buttonStyle">@style/ButtonAppTheme</item>
    </style>

    <!-- Button Style -->
    <style name="ButtonAppTheme" parent="android:Widget.Button">
        <item name="android:background">@drawable/btn_background</item>
        <item name="android:minHeight">48dip</item>
        <item name="android:minWidth">64dip</item>
        <item name="android:textColor">@color/btn_text_color_default</item>

    </style>


    <style name="ActionBar.Solid.Apptheme" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="background">@drawable/ab_solid_apptheme</item>
        <item name="backgroundStacked">@drawable/ab_stacked_solid_apptheme</item>
        <item name="backgroundSplit">@drawable/ab_bottom_solid_apptheme</item>
        <item name="progressBarStyle">@style/ProgressBar.Apptheme</item>
    </style>

    <style name="ActionBar.Transparent.Apptheme" parent="@style/Widget.AppCompat.ActionBar">
        <item name="background">@drawable/ab_transparent_apptheme</item>
        <item name="progressBarStyle">@style/ProgressBar.Apptheme</item>
    </style>

    <style name="PopupMenu.Apptheme" parent="@style/Widget.AppCompat.PopupMenu">
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_apptheme</item>
    </style>

    <style name="DropDownListView.Apptheme" parent="@style/Widget.AppCompat.ListView.DropDown">
        <item name="android:listSelector">@drawable/selectable_background_apptheme</item>
    </style>

    <style name="ActionBarTabStyle.Apptheme" parent="@style/Widget.AppCompat.ActionBar.TabView">
        <item name="android:background">@drawable/tab_indicator_ab_apptheme</item>
    </style>

    <style name="DropDownNav.Apptheme" parent="@style/Widget.AppCompat.Spinner.DropDown.ActionBar">
        <item name="android:background">@drawable/spinner_background_ab_apptheme</item>
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_apptheme</item>
        <item name="android:dropDownSelector">@drawable/selectable_background_apptheme</item>
    </style>

    <style name="ProgressBar.Apptheme" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
        <item name="android:progressDrawable">@drawable/progress_horizontal_apptheme</item>
    </style>

    <style name="ActionButton.CloseMode.Apptheme" parent="@style/Widget.AppCompat.ActionButton.CloseMode">
        <item name="android:background">@drawable/btn_cab_done_apptheme</item>
    </style>

    <!-- this style is only referenced in a Light.DarkActionBar based theme -->
    <style name="Theme.Apptheme.Widget" parent="@style/Theme.AppCompat">
        <item name="popupMenuStyle">@style/PopupMenu.Apptheme</item>
        <item name="dropDownListViewStyle">@style/DropDownListView.Apptheme</item>
    </style>

</resources>

从 MainActivity 创建

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
        getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    // mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
        R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));

    actionBar = getSupportActionBar();
    // Toolbar toolbar =(Toolbar) findViewById(R.id.toolbar);
    // setSupportActionBar(toolbar);

    LayoutInflater mInflater = LayoutInflater.from(this);

    LayoutParams layout = new LayoutParams(
        LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    View mCustomView = mInflater.inflate(R.layout.actionbar, null);
    actionBar.setCustomView(mCustomView,layout);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDefaultDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayUseLogoEnabled(false);
}

任何解决方案可以像以前使用最新的 SDK 一样恢复操作栏?

更新:我可以通过从 Drawerfragment 中的任何位置删除 ActionBarDrawerToggle 来从操作栏中删除箭头。

现在我只面临操作栏的样式问题。 Actionabr 的左侧内边距和背景色

【问题讨论】:

  • ActionBar 现在在 Lollipop 中已被贬低并且存在错误。通过使用工具栏小部件解决。
  • 你从哪里得到它已被弃用的信息(并且有错误:))? Toolbar 是 L 中首选的做事方式,但 ActionBar 绝对不会被弃用..
  • 支持库 v4 中的 ActionBar 已弃用,请改用 v7。

标签: android android-5.0-lollipop android-actionbar-compat android-actionbaractivity


【解决方案1】:

如果您只想要颜色,您应该在主题中将其定义为 primaryColor。看起来很多东西都是为了简单地在 ActionBar 上添加一个色调。

 <style name="Theme.MyTheme" parent="Theme.AppCompat">
    <item name="colorPrimary">@color/primary</item>
 </style>

 <color name="primary">#ff5722</color>

您所指的箭头是getSupportActionBar().setDisplayHomeAsUpEnabled(true|false);,您不再需要(不应该)使用自己的抽屉图标,因为它包含在 appcompat v7 中。 (mDrawerToggle.setDrawerIndicatorEnabled(true);)

【讨论】:

  • 这不是颜色问题。这是填充问题。
【解决方案2】:

这里已经解决了Android Lollipop, AppCompat ActionBar custom view doesn't take up whole screen width

我测试了解决方案并且可以正常工作,但您必须重新设置工具栏的样式

【讨论】:

    猜你喜欢
    • 2015-09-05
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多