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

时间:2016-06-30 11:32:13

标签: android android-layout button android-linearlayout padding

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

假设我在MainActivity.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); } 个{j}代码:

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>

MainActivity.java

输出截图:

NumPy broadcasting

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

enter image description here

但我不想在第二个按钮周围填充。我在S / O中搜索了许多答案,他们总是建议改变背景颜色去除(有人说它是阴影,而不是填充)。所以我在上面secondButton.setBackgroundColor(this.getResources().getColor(android.R.color.holo_red_light)); 取消注释这一行(注意我在这里设置了红色):

table1

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

enter image description here

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

1 个答案:

答案 0 :(得分:1)

尝试

    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);