Android: Drawing a button as a spinner

前端 未结 5 1495
一生所求
一生所求 2020-12-15 05:39

I have a custom android view that extends \'Button\' and acts as a multi-level spinner - when it is pressed it displays a series of spinner-like dialogs that allow a user to

相关标签:
5条回答
  • 2020-12-15 05:45

    This is how I managed to draw a button that is styled like a Spinner:

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

    That's it!

    Update

    Something like the following should also work:

    <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>
    

    For more styles take a peek into the SDK, e.g. sdk/platforms/android-18/data/res/values/styles.xml

    0 讨论(0)
  • 2020-12-15 05:51

    This XML layout makes a button look like a spinner.

    <!-- ****  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" />
    
    0 讨论(0)
  • 2020-12-15 05:57

    This is my solution of drawing button like a spinner:

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

    This is part of my res/values/styles.xml:

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

    This is part of my res/values-v14/styles.xml:

    <style name="MyButton" parent="@android:style/Widget.DeviceDefault.Light.Spinner">
    </style>
    
    0 讨论(0)
  • 2020-12-15 05:59

    I found a solution to my question. In the constructor for my button I set the background drawable resource to be "btn_dropdown".

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

    It looks the same as the spinner buttons, none of the button behavior has changed, and the button text centers correctly.

    0 讨论(0)
  • 2020-12-15 06:06

    You can define the android:background attribute on your button to use a drawable that looks like the spinner. I did this by creating a 9-patch image for the spinner, putting it in my res/drawable folder, and then adding a style for my custom spinner button:

    <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>
    

    For your button, add the style attribute to the XML layout for your layout:

    <Button
      android:id="@+id/okButton"
      style="@style/DialogSpinner"
      android:textColor="#a6a2a2"
      android:text="something"
    />
    
    0 讨论(0)
提交回复
热议问题