2011-12-23 74 views
19

我有一個自定義的android視圖,它擴展了'Button'並充當多級微調 - 當它被按下時,它顯示一系列微調對話框,允許用戶細化他們的選擇。在這種情況下,它允許用戶選擇他們希望拾取發生的日期,然後小時,然後分鐘(以5分鐘爲間隔)。當對話框沒有激活時,按鈕上會顯示當前選擇的文字說明。Android:作爲微調繪製按鈕

我想直觀地表明用戶可以通過按下按鈕來改變選擇,並且認爲微調器的外觀正是我所需要的。我希望外觀與設備上的其他旋鈕相同,並且能夠輕鬆地更改按鈕上的文字,但我不確定如何在沒有它的情況下實現這一點。我應該如何將按鈕顯示爲與設備上其他旋鈕一致的微調像圖標?

回答

7

我找到了我的問題的解決方案。在我的按鈕的構造函數中,我將背景可繪製資源設置爲「btn_dropdown」。

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

它看起來與微調按鈕相同,按鈕行爲沒有改變,並且按鈕文本居中正確。

1

您可以在按鈕上定義android:background屬性,以使用看起來像微調框的drawable。我通過微調創造了9補丁圖像,把它在我的RES /可繪製文件夾,然後添加一個樣式爲我定製的微調按鈕,這樣做:

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

Jonathan Lin的style =「?android:attr/spinnerStyle」看起來更好4.0+ – moberme 2013-08-14 18:14:16

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 /值/ 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> 
+0

我還必須在res/values/styles.xml中添加48dp右填充,否則文本會出現在頂部下拉符號。 – aleb 2013-06-28 11:48:21

47

這是我如何設法畫出一個像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

+6

適用於2.1,2.3,4.0+。 – aleb 2013-06-28 12:54:31