为什么透明笔划显示只有一半宽度?

Why does transparent stroke display with only half of width?

我正在尝试使用 <shape> 来显示按钮的按下状态。为此,我在正常状态下使用透明颜色的笔划,在按下状态下使用彩色笔划:

button.xml :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/button_normal"/>
</selector>

button_pressed.xml :

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#0F0" />
    <stroke android:width="@dimen/size_medium" android:color="#F00" />

    <padding android:left="@dimen/size_medium" android:top="@dimen/size_medium" android:right="@dimen/size_medium"
        android:bottom="@dimen/size_medium" />
</shape>

button_normal.xml :

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#00F" />
    <stroke android:width="@dimen/size_medium" android:color="#0000" />
</shape>

这是我的测试布局:

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/action_sign_in"
    android:textStyle="bold"
    android:layout_marginTop="16dp"
    android:background="@drawable/button"
    android:textColor="#ffffff"
    android:textAlignment="viewStart"
    android:textAppearance="@null"
    android:textSize="15sp"
    android:enabled="true"/>

这就是它的样子

p.s。额外的问题:为什么填充用于两种状态,即使它只设置在按下状态?

笔画在笔画路径的两侧均匀分割。如果有办法 "peel back" 第二张图片上的红色轮廓,您会看到绿色框的大小与第一张图片中的蓝色框完全相同。而你认为第一张图中的"padding"实际上是你透明笔画的一部分。

尝试一个实验:使描边颜色半透明而不是透明,您将能够看到描边如何跨过形状的一半。

在 Shape XML 中似乎没有任何方法可以指定在形状的内部或外部描边,所以如果这确实是您所需要的,您可能必须自己动手Canvas.draw() 在代码中调用。