gpt4 book ai didi

java - 未应用 Android 自定义对话框按钮样式

转载 作者:行者123 更新时间:2023-11-30 10:33:05 25 4
gpt4 key购买 nike

我想为用户创建一个带有 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>

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

它在 Nougat 上运行良好,但是当我在 Lollipop(5.0 和 5.1.1)上运行它时,按钮没有样式,尽管相同的按钮样式适用于 Lollipop 上的 Activity 按钮:

App

我想知道可能出了什么问题,我也尝试将 Dialog 移动到 DialogFragment 中,但我遇到了同样的行为。

最佳答案

找到如下解决方案:

在我的 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>

关于java - 未应用 Android 自定义对话框按钮样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42375295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com