2011-12-23 16 views
19

「ボタン」を拡張し、マルチレベルのスピナーとして機能するカスタムアンドロイドビューを持っています。これを押すと、ユーザーが洗練された一連のスピナー様のダイアログが表示されます彼らの選択。この場合、ユーザーはピックアップを発生させたい日、時、分(5分間隔)を選択することができます。ダイアログがアクティブでない場合、現在選択されているテキストの説明がボタンに表示されます。Android:ボタンをスピナーとして描画

ユーザーがボタンを押して選択を変更できることを視覚的に示し、スピナーの外観がまさに私が必要とするものだと思っています。私はデバイス上の他のスピナーと同じに見えるし、ボタンのテキストを簡単に変更することができますが、私はどのようにkludgyされていないそれを達成するか分からない。デバイス上の他のスピナーと一致するスピナーのようなアイコンとしてボタンを表示するにはどうすればよいですか?

答えて

7

私の質問に対する解決策が見つかりました。私のボタンのコンストラクタでは、バックグラウンドdrawableリソースを "btn_dropdown"に設定しました。

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

スピナーボタンと同じように見えますが、ボタンの動作は変更されておらず、ボタンのテキストは正しく中央になります。

1

ボタンのアンドロイド:背景属性を定義して、スピナーのように見えるドロアブルを使用することができます。私は自分のres /描画可能なフォルダに入れて、スピナーのための9のパッチ画像を作成することによって、これをしなかった、そして私のカスタムスピナーボタンのスタイルを追加:

<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

ジョナサン・リンのstyle = "?android:attr/spinnerStyle"は4.0+の方が良く見えます – moberme

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

47

どのように私はスピナーのようなスタイルのボタンを描くことができましたか:

<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

関連する問題