EditText选中的文本范围指针得到奇怪的背景

时间:2018-01-22 10:05:54

标签: android android-view android-theme

EditText风格

    <style name="MyEditTextStyle" parent="Widget.AppCompat.EditText">
            <item name="android:textCursorDrawable">@null</item>
            <item name="android:textColor">@color/color_states_blackish</item>
            <item name="android:fontFamily">@font/sfd_medium</item>
            <item name="android:background">@drawable/edit_white_gray_x_normal_rounded</item>
            <item name="android:height">@dimen/general_view_height</item>
        </style>

对话样式

 <style name="MyAlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
        <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
    </style>
    <style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
        <item name="android:textColor">@color/text_color_blackish</item>
        <item name="android:fontFamily">@font/sfd_regular</item>
    </style>

    <style name="PositiveButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
        <item name="android:textColor">@color/text_color_blackish</item>
        <item name="android:fontFamily">@font/sfd_bold</item>
    </style>

对话框代码

final EditText editText = new EditText(new android.view.ContextThemeWrapper(getContext(),R.style.MyEditTextStyle));
                        final FrameLayout layout = new FrameLayout(getContext());
                        int padding = getContext().getResources().getDimensionPixelSize(R.dimen.standard_wall_space);
                        int paddingView = getContext().getResources().getDimensionPixelSize(R.dimen.standard_view_space);
                        layout.setPadding(padding,paddingView,padding,paddingView);
                        layout.addView(editText);
                        editText.setHint(R.string.type_here);
                        new AlertDialog.Builder(getContext(),R.style.MyAlertDialogTheme)
                        .setTitle("Enter Your Unit")
                        .setView(layout)
                        .setPositiveButton("Add", (dialog, whichButton) -> {
                            String value = editText.getText().toString();
                            MyApp.showToast("Unit-"+value);
                        })
                        .setNegativeButton("Cancel", null).show();

我得到了一些与EditText相同的背景指针,我将它们删除了ContextThemeWrapper然后它显示正确,但我需要使用样式更改背景?,为什么会发生这种情况?这是全屏对话框内的对话框?有解决方案吗感谢

enter image description here

3 个答案:

答案 0 :(得分:0)

看起来像bugg,选择器弹出窗口出错背景样式,不要使用ContextThemeWrapper,样式然后正常显示,以编程方式设置背景。

 final EditText editText = new EditText(getContext());
editText.setBackgroundResource(R.drawable.edit_white_gray_x_normal_rounded);
editText.setTextColor(ContextCompat.getColor(getContext(),R.color.color_states_blackish));

答案 1 :(得分:0)

问题是主题中的android:background参数会应用于EditText内的所有内容。对我来说,解决方案是从样式中删除该参数。

答案 2 :(得分:0)

除了此处的其他答案外,当我们在style.xml中的应用程序主题中设置背景时也会发生。我发现的另一种解决方案是将android:background替换为背景颜色的android:backgroundTint,并将android:backgroundTintMode设置为multiple

将此样式设置为

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
    <item name="android:backgroundTint">#FFFFFF</item>
    <item name="android:backgroundTintMode">multiply</item>
</style>

或在您要更改其背景颜色的视图标签中:

<View
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="#FFFFFF"
    android:backgroundTintMode="multiply" />