【问题标题】:Android: Drawing a button as a spinnerAndroid:将按钮绘制为微调器
【发布时间】:2012-01-27 00:30:45
【问题描述】:

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

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

【问题讨论】:

    标签: android android-widget android-spinner android-button


    【解决方案1】:

    这就是我设法绘制一个样式类似于 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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        【解决方案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>
        

        【讨论】:

        • 我还必须在 res/values/styles.xml 中添加 48dp 的右内边距,否则文本会出现在下拉符号的顶部。
        【解决方案4】:

        这种 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" />
        

        【讨论】:

        • Jonathan Lin 的 style="?android:attr/spinnerStyle" 在 4.0+ 上看起来更好
        【解决方案5】:

        您可以在按钮上定义 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"
        />
        

        【讨论】:

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