如何创建一个Widget.Holo.Spinner风格的小部件V7创建一个、部件、风格、Widget

2023-09-06 13:40:57 作者:你满身是刺我也会紧紧抱住

我有一个按钮,我想样式就像一个全息式的微调,即强调用三角形右下角 - 为内置的联系人应用程序例如,做到这一点。

I have a Button that I want to style like a Holo-style spinner, i.e. underlined with a triangle in the bottom right corner - the built-in contacts app for example does that.

最简单的方法是创建按钮和风格为风格=@安卓风格/ Widget.Holo.Spinner - 这给我的视觉效果我想要的。

The easiest way is to create the button and style it as style="@android:style/Widget.Holo.Spinner" - that gives me the visual result I want.

不过,我的应用程序是为了支持Android 2.X,所以我不能使用本机的Holo风格。 AppCompat有几个微调相关的款式,如 @风格/ Widget.AppCompat.Spinner 等人,但他们不看一样的 - 他们只是创造一个小三角,在中心在小窗口的右边缘,而没有下划线

However, my app is meant to support Android 2.x, so I can't use native Holo styles. AppCompat has several spinner-related styles, like @style/Widget.AppCompat.Spinner and others, but they don't look the same - they just create a little triangle in the center at the right edge of the widget, and no underline.

我会做傻事它通过创建自己的9个补丁的背景,但我无法想象,这个全息微调风格是从V7的AppCompat失踪。有没有一种风格,我可以使用,或任何其他方式来创建视觉效果我想在不创造我自己的绘制资源?

I could fudge it by creating my own 9-patch background, but I can't imagine that this Holo spinner style is missing from v7's AppCompat. Is there a style I can use, or any other way to create the visual result I want without creating my own drawable resource?

推荐答案

用户OZI。

诀窍就是使用自定义属性,并用V11-特定部分覆盖它。这里是基本的设置,如果你使用的主题:

The trick is to use a custom attribute and override it with a v11-specific section. Here's the basic setup if you use themes:

在值/ attr.xml:

In values/attr.xml:

<declare-styleable name="MyApp">
    <attr name="holoSpinnerStyle" format="reference" />
</declare-styleable>

您创建的旋按钮是这样的:

You create your Spinner-Button like this:

<Button
    style="?attr/holoSpinnerStyle"
     />

而这里的关键是:你有不同的价值观的主题取决于你是否拥有V11:

And here's the key: You have different values in the theme depending on whether or not you have v11:

在值/的themes.xml:

In values/themes.xml:

<style name="MyTheme">
    <item name="holoSpinnerStyle">@style/Widget.AppCompat.Spinner</item>
</style>

在值-V11 /的themes.xml:

In values-v11/themes.xml:

<style name="MyTheme">
    <item name="holoSpinnerStyle">@android:style/Widget.Holo.Light.Spinner</item>
</style>

如果您不使用主题,您的设置几乎是相同的 - 你只需要使用styles.xml,创建一个特定的风格为您的按钮,并使用

If you don't use themes, your setup is almost identical - you just use styles.xml, create a specific style for your button, and use that.

在pre-V11,这将创建一个旧式的微调,但是这实际上是好 - 一个V11微调会看出来的地方在pre-V11环境

In pre-v11, this will create an old-style spinner, but that's actually good - a v11 spinner would look out of place in pre-v11 environment.