2017-02-21 3 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

には、次の解決策が見つかりました:

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

をそして、私のカスタムダイアログのレイアウトでは、私は私のボタンのテーマとして、そのスタイルを使用:私のstyles.xmlで

を、私は追加

<?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> 
関連する問題