2014-09-11 91 views
0

我使用此代碼創建了一些選擇器 - Number Picker號碼選取器隨着警報對話框如按鈕

但輸出看起來是這樣的 -

enter image description here

但我希望它看起來像 -

enter image description here

我想要顯示的取消和設置按鈕爲在圖像中給出。

但是,當我嘗試此代碼,它顯示錯誤 - 方法setButton(int,字符串,對話框)是未定義的類型對話框。

Dialog noPicker = new Dialog(MainActivity.this); 
noPicker.setButton(Dialog.BUTTON_POSITIVE, "Ok", noPicker); 
noPicker.setButton(Dialog.BUTTON_NEGATIVE, "Cancel", noPicker); 

有關如何解決此問題的任何想法?

+0

我們需要她更多的代碼,你能不能請發表您的layout.xml – Westranger 2014-09-11 12:01:00

+0

的XML佈局有中給出的鏈接。無論如何這是 - http://stackoverflow.com/questions/17805040/how-to-create-a-number-picker-dialog/17806895#17806895 – Confuse 2014-09-11 12:01:33

+0

使用DialogFragment與數字選擇器http://developer.android.com/指南/主題/ UI/dialogs.html#DialogFragment – mcd 2014-09-11 12:20:57

回答

0

這個想法是將按鈕換成線性佈局,並放置在底部。那麼你需要平均的重量按鈕。該代碼必須是與此類似:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <NumberPicker 
     android:id="@+id/numberPicker1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="64dp" /> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentBottom="true" 
     android:weightSum="2" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Cancel" /> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Set" /> 

    </LinearLayout> 
</RelativeLayout> 
0

設置對話框的主題,它應該工作:下面的示例代碼可以幫助

Dialog dialog=new Dialog(context, theme) 

和style.xml文件中定義的主題。這將是這樣的:

<style name="DoneCancelBar" parent="android:Theme.Light"> 
    <item name="android:layout_width">match_parent</item> 
    <item name="android:layout_height">match_parent</item> 
    <item name="android:orientation">horizontal</item> 
</style>