Android: 防止背景颜色向形状边缘变化

Android: prevent background color from changing toward the edge of a shape

我需要创建带有红色边框(宽度 1px)的白色矩形形状,并从下方附加另一个红色矩形。像这样:

白色矩形将 EditText 而红色边框和红色矩形将出现,以通知用户输入错误。没有什么花哨。

我已经尝试了几种方法,但由于相同的原因,其中 none 看起来不错 - 颜色朝着形状的边缘变化(看起来最外面的像素是变化的)。可以在这张放大图片中看到问题:

这个特定快照的层次结构是 LinearLayout 红色背景和 padding 1px,但我在尝试使用其他方法(包括绘图)时也观察到相同的效果EditText 周围的边框以编程方式)。

这个效果看起来像Android的标准,我的问题是如何禁用它?

查看以下示例,希望对您有所帮助。

rect_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <stroke
        android:width="3dp"
        android:color="#EF9A9A"/>
    <solid android:color="@android:color/white"/>

</shape>

layout.xml

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/rectangle_shape"
        android:orientation="vertical">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:lines="3"
            android:padding="5dp"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:background="#EF9A9A"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="36dp"
            android:layout_marginBottom="3dp"
            android:layout_marginLeft="3dp"
            android:layout_marginRight="3dp"
            android:background="#EF5350"/>


    </LinearLayout>

输出:

以防万一有人偶然发现这个问题 - 所描述的行为是一些与模拟器相关的工件。在真实设备上,颜色很好。