2017-02-21 30 views
0

我想爲用戶創建一個簡單的對話框,2個按鈕如下:的Android定製對話框按鈕樣式不施加

對話框佈局(dialog_layout.xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="8dp" 
    android:orientation="vertical"> 

    <Button 
     android:id="@+id/btn_select_choice_1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="First Choice" 
     android:theme="@style/secondary_button_normal" /> 

    <Button 
     android:id="@+id/btn_select_choice_2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Second Choice" 
     android:theme="@style/secondary_button_normal" /> 
</LinearLayout> 

secondary_button_normal:

<style name="secondary_button_normal" parent="Widget.AppCompat.Button"> 
     <item name="colorButtonNormal">@color/button_secondary_normal_background</item> 
     <item name="android:textColor">@color/button_secondary_normal_text</item> 
     <item name="android:textSize">@dimen/button_textSize</item> 
     <item name="android:padding">@dimen/button_padding</item> 
</style> 

活動的onCreate:

final Dialog selection = new Dialog(this); 
     selection.setContentView(R.layout.dialog_layout); 
     Button selectFirstChoice = (Button)selection.findViewById(R.id.btn_select_choice_1); 
     Button selectSecondChoice = (Button)selection.findViewById(R.id.btn_select_choice_2); 
     selectFirstChoice.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //do something 
       selection.dismiss(); 
      } 
     }); 
     selectSecondChoice.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //do something 
       selection.dismiss(); 
      } 
     }); 
     selection.setTitle("Some Title"); 
     selection.setCancelable(false); 
     selection.show(); 

預覽是好的:

Preview

它運作良好的牛軋糖,但是當我的棒棒堂(5.0和5.1.1)運行它,按鈕是沒有的造型,雖然相同的按鈕造型上的活動按鈕製作棒棒糖:

App

我想這可能是出錯了,我也嘗試將對話框移到DialogFragment中,但我面對同樣的行爲。

+0

你有沒有試過把你的風格放在文件夾res/values-v21/styles.xml中。也? – Mohamed

+0

@穆罕默德我嘗試過,但無濟於事。 – linkinu

回答

0

發現了以下解決方案:

在我Styles.xml,我說:

<style name="dialog_button" parent="Theme.AppCompat.Light.Dialog"> 
    <item name="colorButtonNormal">@color/button_secondary_normal_background</item> 
    <item name="android:textColor">@color/button_secondary_normal_text</item> 
    <item name="android:textSize">@dimen/button_textSize</item> 
    <item name="android:padding">@dimen/button_padding</item> 
</style> 

而且在我的自定義對話框的佈局,我用這種風格作爲我的按鈕主題:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_margin="8dp" 
    android:layout_height="match_parent"> 
<Button 
    android:id="@+id/btn_select_student" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/student" 
    android:theme="@style/dialog_button" /> 
<Button 
    android:id="@+id/btn_select_tutor" 
    android:layout_width="match_parent" 
    android:text="@string/tutor" 
    android:layout_height="wrap_content" 
    android:theme="@style/dialog_button" /> 
</LinearLayout>