如何删除 setBackgroundTintList() 的按钮填充

How to remove button padding of setBackgroundTintList()

我尝试通过java(第二个按钮)模拟选择器xml(第一个按钮)。

假设我在 MainActivity.java:

中有这个 onCreate() java 代码
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button secondButton = (Button) findViewById(R.id.buttonJavaSelector);
    int[][] hole_states = new int[][] {

            new int[]{android.R.attr.state_pressed},
            new int[]{}
    };
    int[] hole_colors = new int[] {
            ContextCompat.getColor(this, android.R.color.white),
            ContextCompat.getColor(this, android.R.color.holo_green_light)
    };
    ColorStateList holeStateList = new ColorStateList(hole_states, hole_colors);
    //secondButton.setBackgroundColor(this.getResources().getColor(android.R.color.holo_red_light));
    secondButton.setBackgroundTintList(holeStateList);
    secondButton.setHighlightColor(Color.RED);
}

然后activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.blogspot.diannaoxiaobai.tmptesting.MainActivity">

    <LinearLayout
        android:id="@+id/layout_sign_in"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/yes_part"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <Button
                android:id="@+id/buttonXmlSelector"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/button_ripple_light"
                android:text="Cancel (Xml Selector)"
                android:textColor="@android:color/holo_blue_light" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/no_part"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <Button
                android:id="@+id/buttonJavaSelector"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Sign In (Java Selector)"
                android:textColor="@android:color/holo_blue_light" />

        </LinearLayout>

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

然后drawable/button_ripple_light.xml:

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

输出截图:

如果我按下两个按钮,都可以变成白色:

但我不想要第二个按钮周围的填充。我在 S/O 中搜索了很多答案,他们总是建议更改背景颜色以删除(有人说这是阴影,而不是填充)。所以我在上面的 MainActivity.java 中取消注释这一行(注意我在这里设置了红色):

secondButton.setBackgroundColor(this.getResources().getColor(android.R.color.holo_red_light));

现在填充不再存在,但是当我按下第二个按钮时没有任何反应(即变成白色):

用于删除填充的 setBackgroundColor() 无法正常工作。在这种情况下,我应该怎么做才能删除第二个按钮填充?

尝试

    StateListDrawable holeStateList = new StateListDrawable();
    holeStateList.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(ContextCompat.getColor(this,
            android.R.color.white)));
    holeStateList.addState(new int[]{}, new ColorDrawable(ContextCompat.getColor(this,
            android.R.color.holo_green_light)));

    secondButton.setBackground(holeStateList);