21

我有一个自定义的 android 视图,它扩展了“按钮”并充当多级微调器 - 当它被按下时,它会显示一系列类似于微调器的对话框,允许用户优化他们的选择。在这种情况下,它允许用户选择他们想要取件的日期、小时、分钟(以 5 分钟为间隔)。当对话框未激活时,当前选择的文本描述会显示在按钮上。

我想直观地表明用户可以通过按下按钮来更改选择,并认为微调器的外观正是我所需要的。我希望外观与设备上的其他微调器相同,并且能够轻松更改按钮上的文本,但我不确定如何在不笨拙的情况下实现这一点。我应该如何将按钮显示为与设备上其他微调器一致的微调器图标?

4

5 回答 5

51

这就是我设法绘制一个样式类似于 Spinner 的按钮的方法:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    style="?android:attr/spinnerStyle" />

就是这样!

更新

像下面这样的东西也应该起作用:

<style name="SpinnerButtonStyle" parent="android:Widget.Holo.Spinner">
    <item name="android:textColor">@color/entry_field</item>
    <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
</style>

如需更多样式,请查看 SDK,例如sdk/platforms/android-18/data/res/values/styles.xml

于 2013-05-21T06:27:32.750 回答
7

我找到了我的问题的解决方案。在我的按钮的构造函数中,我将背景可绘制资源设置为“btn_dropdown”。

public DateAndTimePicker(Context context) {
    super(context);
    this.setBackgroundResource(android.R.drawable.btn_dropdown);
}

它看起来与微调按钮相同,没有任何按钮行为发生变化,并且按钮文本正确居中。

于 2011-12-28T18:27:31.293 回答
3

这种 XML 布局使按钮看起来像一个微调器。

<!-- ****  Button styled to look like a spinner  **** -->
    <Button
        android:id="@+id/btn_category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:drawable/btn_dropdown"
        android:gravity="center_vertical|left" 
        android:textSize="18sp" />
于 2012-05-10T15:56:02.050 回答
3

这是我像微调器一样绘制按钮的解决方案:

<Button
    android:id="@+id/my_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="12dp"
    android:gravity="center_vertical|left"
    android:enabled="true"
    android:text="@string/my_button_text"
    android:textColor="@color/my_button_color"
    style="@style/MyButton" />

这是我的 res/values/styles.xml 的一部分:

<style name="MyButton" parent="@android:style/Widget.Spinner">
</style>

这是我的 res/values-v14/styles.xml 的一部分:

<style name="MyButton" parent="@android:style/Widget.DeviceDefault.Light.Spinner">
</style>
于 2013-01-07T00:43:36.797 回答
1

您可以在按钮上定义 android:background 属性以使用看起来像微调器的可绘制对象。为此,我为微调器创建了一个 9-patch 图像,将其放在我的 res/drawable 文件夹中,然后为我的自定义微调器按钮添加样式:

<style name="DialogSpinner">
  <item name="android:background">@drawable/dlg_spinner_background</item>
  <item name="android:textColor">#919090</item>
  <item name="android:layout_height">wrap_content</item>
  <item name="android:layout_width">wrap_content</item>
  <item name="android:layout_marginTop">2px</item>
  <item name="android:layout_marginBottom">2px</item>
</style>

对于您的按钮,将样式属性添加到您的布局的 XML 布局中:

<Button
  android:id="@+id/okButton"
  style="@style/DialogSpinner"
  android:textColor="#a6a2a2"
  android:text="something"
/>
于 2011-12-27T20:08:41.510 回答