简体   繁体   English

Android ActionBar样式问题

[英]Android ActionBar styling issues

I want to have custom background for the menu items displayed in the ActionBar . 我想为ActionBar显示的菜单项设置自定义背景。 I found out that I can do that by adding the following item to my app theme style definition: 我发现可以通过在我的应用主题样式定义中添加以下内容来做到这一点:

    <item name="android:actionBarItemBackground">@drawable/actionbar_button</item>

Where actionbar_button.xml contains: 其中actionbar_button.xml包含:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/action_btn_on" android:state_pressed="true" />
    <item android:drawable="@drawable/action_btn_off" />
</selector>

However, this causes the background image to also render behind the custom app logo displayed on the left of the ActionBar. 但是,这会导致背景图像也呈现在ActionBar左侧显示的自定义应用程序徽标后面。 After several attempts at figuring out a selector that would include only the buttons, I came up with this: 经过几次尝试找出仅包含按钮的选择器,我想到了:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/action_btn_on" android:state_pressed="true" android:state_enabled="true" />
    <item android:drawable="@drawable/action_btn_off" android:state_pressed="false" android:state_enabled="true"/>
    <item android:drawable="@android:color/transparent"/>
</selector>

Which sort of worked, and after the app loads fully the background behind the logo is transparent as I want it. 哪种工作方式以及应用程序完全加载后,徽标后面的背景都是我想要的透明的。 But the problem is that while it's still loading it briefly shows the logo with the button background behind it and more annoyingly, it scales and crops the logo to fit inside the fill area of the 9-patch graphic that is the button background. 但是问题在于,尽管它仍在加载,但会短暂地显示徽标以及其背后的按钮背景,而且更令人讨厌的是,它会缩放并裁剪徽标以适合9补丁图形(该按钮背景)的填充区域。 When finished loading, the background is switched out, but the scaling and cropping remains, which looks quite ugly. 加载完成后,背景被关闭,但是缩放和裁剪仍然存在,这看起来很丑陋。 How can I fix it? 我该如何解决?

Have not found a working solution to the general problem of setting image-based backgrounds to ActionBar buttons, but the scaling/cropping does not happen if I use entered XML based backgrounds, such as created with a layer-list of shapes. 尚未找到将基于图像的背景设置为ActionBar按钮的一般问题的可行解决方案,但是如果我使用输入的基于XML的背景(例如使用形状的layer-list创建),则不会发生缩放/裁剪。 For some cases in may not be viable, but in my case it was entirely possible, and maybe even better than using a bitmap based resource. 在某些情况下,这可能并不可行,但在我看来,这是完全可能的,甚至比使用基于位图的资源更好。

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

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