简体   繁体   English

AppCompat Actionbar样式

[英]AppCompat Actionbar styling

I'm currently trying to use AppCompat instead of ActionbarSherlock. 我目前正在尝试使用AppCompat而不是ActionbarSherlock。 On Android 4+ devices I don't run into any problems, as the normal Actionbar and Holo theme are used. 在Android 4+设备上,我没有遇到任何问题,因为使用了正常的Actionbar和Holo主题。

ICS行动栏

On lower Android versions however, this weird look happens when using Theme.AppCompat.Light. 然而,在较低的Android版本上,使用Theme.AppCompat.Light时会出现这种奇怪的外观。

标准AppCompat Actionbar

The Actionbar is white and has a blue line underneath. Actionbar为白色,下方有蓝线。

I was able to fix that by using the Android Action Bar Style Generator from Android Asset Studio to generate a custom theme with a solid Actionbar instead of the transparent one. 我能够通过使用Android Asset Studio中的Android Action Bar Style Generator来生成一个自定义主题,其中包含一个实心的Actionbar而不是透明的ActionB。

AppCompat与Style Generator的自定义绘图

When comparing it to the Actionbar I got when using ActionbarSherlock, it's still kind of ugly though. 将它与使用ActionbarSherlock时获得的Actionbar进行比较时,它仍然有点难看。

ActionbarSherlock

The AppCompat Actionbar has this very sharp, black line between itself and the content of the activity, while the stock Actionbar of ICS or the one of ActionbarSherlock have a softer transition from the Actionbar to the layout. AppCompat Actionbar在它自身与活动内容之间有一条非常清晰的黑线,而ICS的Actionbar或ActionbarSherlock的一条动作条从Actionbar到布局的转换更为柔和。

How can I style the Actionbar to really look like the one on Android 4+? 我如何设计Actionbar的样式,看起来真的像Android 4+上的那个?

You have to create your own style with the image you want to put on background. 您必须使用要放在背景上的图像创建自己的样式。 In your style.xml, you should create something similar to this: 在style.xml中,您应该创建类似于此的东西:

values/styles.xml 价值观/ styles.xml

    <style name="Theme.MyAppTheme" parent="@style/Theme.AppCompat.Light">
        <item name="actionBarStyle">@style/ActionBarTabStyle.MyAppTheme</item>
    </style>

    <style name="ActionBar.Solid.MyAppTheme" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
        <item name="background">@drawable/your_background_image</item>
    </style>

values-v14/styles.xml 值-V14 / styles.xml

    <style name="Theme.MyAppTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:actionBarStyle">@style/ActionBarTabStyle.MyAppTheme</item>
    </style>

    <style name="ActionBar.Solid.MyAppTheme" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
        <item name="android:background">@drawable/your_background_image</item>
    </style>

and you can use this 9-patch image : 你可以使用这个9补丁图像:

9补丁图像

I use this code: 我用这个代码:

<resources>

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="actionBarStyle">@style/ActionBarTheme</item>
</style>

<style name="ActionBarTheme" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
    <item name="background">@color/red_action_bar</item>
</style>

</resources>

I hope this code is of great help. 我希望这段代码有很大的帮助。

您必须覆盖操作栏的主题

To show custom icon & color in AppCompatActivity we can paste this code: 要在AppCompatActivity中显示自定义图标和颜色,我们可以粘贴此代码:

values/styles.xml 价值观/ styles.xml

<resources>

 <style name="Theme.MyAppTheme" parent="@style/Theme.AppCompat">
    <item name="actionBarStyle">@style/ActionBar.Solid.MyAppTheme</item>
    <item name="icon">@drawable/ic_launcher</item>
</style>

<style name="ActionBar.Solid.MyAppTheme" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
    <item name="background">@color/white</item>
</style>

values-v14/styles.xml 值-V14 / styles.xml

<resources>

<style name="Theme.MyAppTheme" parent="@style/Theme.AppCompat.Light">
    <item name="android:actionBarStyle">@style/ActionBar.Solid.MyAppTheme</item>
       <item name="icon">@drawable/ic_launcher</item>
</style>

<style name="ActionBar.Solid.MyAppTheme" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
    <item name="android:background">@color/app_titile_bar_color</item>
</style>

You can use your custom background picture as well from @drawable/your_pic.png 您也可以使用@ drawable / your_pic.png中的自定义背景图片

To show app icon & Text you also paste this to your activity. 要显示应用图标和文字,您还可以将其粘贴到您的活动中。

        // set action Bar icon & Text
    ActionBar mActionBar = getSupportActionBar();
    mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE
            | ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM