我希望为用户创建一个简单的对话框,其中包含2个按钮,如下所示:
对话框布局(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();
预览没问题:
它在Nougat上运行良好,但是当我在Lollipop(5.0和5.1.1)上运行它时,按钮没有样式,虽然相同的按钮样式适用于Lollipop上的活动按钮:
我想知道可能出现什么问题,我也尝试将Dialog移动到DialogFragment中,但我遇到了同样的行为。
答案 0 :(得分:1)
找到以下解决方案:
在我的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>