按钮背景透明化

211

我有一个按钮。当我按下按钮时,我必须将文本设置为加粗或普通样式。所以我编写了加粗和普通文本的样式。

<style name="textbold" parent="@android:style/TextAppearance">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textStyle">bold</item>
</style>
<style name="textregular" parent="@android:style/TextAppearance">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textStyle">normal</item>
</style>

现在我有一个button_states.xml文件:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
    style="@style/textbold" />
<item android:state_focused="false" android:state_pressed="true"
    style="@style/textregular" />

<item style="@style/textregular" />
</selector> 

在这个按钮的布局中,我还需要将背景设置为透明...我该怎么做?我的布局代码如下:

<Button android:id="@+id/Btn" android:background="@drawable/button_states" />

如何在样式中将背景设置为透明?


4
这么多好回答现在都可以被接受了... - QED
请查看此处的答案,了解如何制作带边框的透明按钮:https://dev59.com/0GnWa4cB1Zd3GeqP1YMy - Basheer AL-MOMANI
为什么不直接使用TextView,并在其onClick事件中调用该操作呢? - Dula wnRp
15个回答

399
要使背景透明,请使用android:background="@android:color/transparent"
然而,你的问题似乎更深层次,因为你正在以一种非常奇怪的方式使用选择器。你目前使用的方式似乎是错误的,尽管如果它确实有效,那么你应该将背景图像放在样式中作为一个<item/>
请仔细查看Android源代码中如何使用样式。虽然它们不会在点击按钮时更改文本样式,但是有很多好的想法可以实现你的目标。

1
请看我的澄清答案。我一开始误读了你的问题。背景在样式中设置。 - Steve Pomeroy
抱歉,我不太清楚解决方案。仅仅设置@android:color/transparent并不支持普通按钮具有的触摸样式(等同于将背景设置为@null)。 - gatoatigrado
@gatoatigrado:您的意思是希望透明区域用橙色轮廓线或阴影突出显示吗?如果是,您需要创建自定义的9-patch图像来实现。 - Steve Pomeroy
@StevePomeroy,是的,在Android 4.1上默认的按钮效果是在[按下按钮]时背景会变成浅蓝色,并带有更浅的蓝色轮廓。 - gatoatigrado
@gatoatigrado:好的。如果你想要那样做,那么你不能使用一个透明的按钮。我不确定你在尝试实现什么。 - Steve Pomeroy
在Android Studio BumbleBee中,只有@MuhammadAamirAli的答案似乎有效。 - Payel Senapati

141
尝试新的方法设置背景透明。
    android:background="?android:attr/selectableItemBackground"

9
我建议使用这个。这样,您可以保留美丽的材料设计动画按钮 =) - Ryan Newsom
这是更好的答案。 - Marcos Dávalos
如果想要圆形的行为,请使用:android:background="?android:attr/selectableItemBackgroundBorderless" - Andrew

38

您也可以在XML中使用以下代码:

android:background="@null"

或者在代码中:

buttonVariable.setBackgroundColor(Color.TRANSPARENT);

点赞给出替代方案,很高兴看到所有的选择! - Burkely91

35

在你的Xml中添加以下代码 - android:background="@android:color/transparent"

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Button"
            android:background="@android:color/transparent"
            android:textStyle="bold"/>

35

使用 #0000(只有四个零,否则会被视为黑色)这是透明的颜色代码。您可以直接使用它,但我建议您在color.xml中定义一个颜色,这样您就可以享受代码的可重复使用性。


10
幸运的是,这已经为我们定义为“@android:color/transparent”。 - Richard Le Mesurier

31
我用XML实现了这个功能。
android:background="@android:color/transparent"

12

我使用了

btn.setBackgroundColor(Color.TRANSPARENT);

android:background="@android:color/transparent"

btn.setBackgroundColor(Color.TRANSPARENT); 对我来说运行良好。 - jay_mziray

9

选择器仅适用于可绘制对象,而不适用于样式。 参考链接


首先,要使按钮背景透明,请使用以下属性,因为这不会影响材料设计动画:

style="?attr/buttonBarButtonStyle"

有很多方法可以设计你的按钮。查看这个教程

其次,要使文本在按下时加粗,使用以下Java代码:

btn.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {
            // When the user clicks the Button
            case MotionEvent.ACTION_DOWN:
                btn.setTypeface(Typeface.DEFAULT_BOLD);
                break;

            // When the user releases the Button
            case MotionEvent.ACTION_UP:
                btn.setTypeface(Typeface.DEFAULT);
                break;
        }
        return false;
    }
});

5
我们可以在

1
步骤1:在drawable中创建一个新的资源文件,然后复制粘贴。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <stroke android:color="#fff" android:width="2dp"/>
    <corners android:radius="25dp"/>
    <padding android:right="15dp" android:top="15dp" android:bottom="15dp" android:left="15dp"/>
</shape>

将其保存为ButtonUI(假设)

步骤2:将UI应用于按钮XML

<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="join the crew"
  android:background="@drawable/ButtonUI"
  android:textColor="#fff"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接