Android 改变 AlertDialog 元素的颜色

问题描述 投票:0回答:1

我想仅使用 xml 更改 AlertDialog 元素的颜色。

我想用xml改变的元素是:

  1. AlertDialog 的背景颜色
  2. 网址文字颜色
  3. 焦点时文本输入的线条颜色
  4. 两个 AlertDialog 按钮上的文本颜色

我希望 AlertDialog 的大小保持不变。

我尝试使用样式:How can I change default dialog button text color in android 5

但是它们不起作用,并且 AlertDialog 的大小发生变化,它缩小了。

你能帮我一下吗?

public void AlertDialog() {
        View viewInflated = LayoutInflater.from(MainActivity.this).inflate(R.layout.text_input, null);
        final EditText input = (EditText) viewInflated.findViewById(R.id.input);
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Url");
        builder.setView(viewInflated);
        builder.setPositiveButton(android.R.string.ok, (dialog, which) -> {
            dialog.dismiss();
            String text = input.getText().toString();
            if (!text.equals("")) pinAppWidget(text);
        });
        builder.setNegativeButton(android.R.string.cancel, (dialog, which) -> dialog.cancel());
        builder.show();
    }

text_input.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="4dp">

    <AutoCompleteTextView
        android:id="@+id/input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Text"
        android:imeOptions="actionDone"
        android:inputType="text" />

</FrameLayout>

值/themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.Test" parent="Theme.Material3.DayNight.NoActionBar">
        <!-- Customize your light theme here. -->
        <!-- <item name="colorPrimary">@color/my_light_primary</item> -->
        <item name="android:statusBarColor">@color/colorPrimary</item>
    </style>

    <style name="Theme.Test" parent="Base.Theme.Test" />
</resources>

values-night/themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.Test" parent="Theme.Material3.DayNight.NoActionBar">
        <!-- Customize your dark theme here. -->
        <!-- <item name="colorPrimary">@color/my_dark_primary</item> -->
        <item name="android:statusBarColor">@color/colorPrimary</item>
    </style>
</resources>
java android android-studio android-alertdialog android-xml
1个回答
0
投票

你如何限制这个只有 xml 文件,你可以盯着你的主题。您将需要创建一个继承所需特征的新主题,让我们使用

Theme.MaterialComponents.DayNight.Dialog.Alert
为您服务。然后覆盖 AlertDialog 的主题默认属性。你可以做到这一点。

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.Test" parent="Theme.Material3.DayNight.NoActionBar">
    <!-- Customize your light theme here. -->
    <!-- <item name="colorPrimary">@color/my_light_primary</item> -->
    <item name="android:statusBarColor">@color/colorPrimary</item>
    <item name="alertDialogTheme">@style/Theme.CustomThemeAlertDialog</item>
</style>
<style name="Theme.CustomThemeAlertDialog" parent="Theme.MaterialComponents.DayNight.Dialog.Alert">
    <!--Background color-->
    <item name="android:background">#0000ff</item>

    <!--tittle color-->
    <item name="android:textColor">#000000</item>

    <!--text color of message-->
    <item name="android:textColorPrimary">#ff0000</item>

    <!--text color of buttons-->
    <item name="colorPrimary">#FFFF00</item>
</style>
<style name="Theme.Test" parent="Base.Theme.Test" />

一个简单的 AlertDialog 的默认结果就是它(没有您的自定义视图)

并完成在您的

android:textColor="@color/color_text_url"
中添加属性
android:backgroundTint="@color/color_line_input_url"
(用于Url文本颜色)和
AutoCompleteTextView

(用于textInput的线条颜色)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp">

<AutoCompleteTextView
    android:id="@+id/input"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Text"
    android:imeOptions="actionDone"
    android:inputType="text"
    android:textColor="@color/color_text_url"
    android:backgroundTint="@color/color_line_input_url" />

</FrameLayout>

那是

© www.soinside.com 2019 - 2024. All rights reserved.