按钮背景为透明


177

我有一个按钮。当我按下按钮时,我必须将文本设置为粗体,否则会正常显示。因此,我为粗体和普通写样式。

<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" />

如何在样式中加入透明背景?


3
如此多的好答案现在可以被接受...
QED 2015年

在这里查看此答案 以使带有边框的透明按钮
Basheer AL-MOMANI

'为什么不只使用TextView并在onClick上调用它呢?
Dula wnRp '16

Answers:


360

要使背景透明,只需执行即可android:background="@android:color/transparent"

但是,您的问题似乎更深了,因为您以一种非常奇怪的方式使用选择器。您使用它的方式似乎是错误的,尽管如果它确实有效,则应将背景图像作为放置在样式中<item/>

仔细研究一下Android源代码中样式的使用方式。尽管他们不会在单击按钮时更改文本样式,但是有很多关于如何在此处实现目标的好主意。


1
看看我澄清的答案。我误解了您的问题,开始了。背景设置为样式。
史蒂夫·波默罗伊

抱歉,我不清楚解决方案。仅设置@android:color / transparent不支持普通按钮具有的触摸样式(并且等效于将背景设置为@null)。
gatoatigrado 2012年

@gatoatigrado:您是说要透明区域以橙色轮廓显示还是以某些阴影显示?如果是这样,则需要为此创建自定义的9补丁图像。
史蒂夫·波默罗伊

@StevePomeroy,是的,Android 4.1上的默认按钮效果是[按按钮时]背景为浅蓝色,蓝色轮廓更亮。
gatoatigrado 2012年

@gatoatigrado:好的。如果需要,则不能使用透明按钮。我不确定您要在此完成什么。
史蒂夫·波默罗伊


35

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


9
幸运的是,这已经为我们定义了@android:color/transparent
Richard Le Mesurier 16'1

34

将此添加到您的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"/>

32

您还可以在xml中使用:

android:background="@null"

或代码中:

buttonVariable.setBackgroundColor(Color.TRANSPARENT);

支持提供替代方案,喜欢看到所有的选择!
Burkely91 '16



8

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


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

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

1

您可以通过设置颜色Alpha通道来实现。

配色方案像这样#AARRGGBB,其中A代表alpha通道(透明度),R代表红色,G代表绿色,B代表蓝色。


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"/>

0

码:

button.setVisibility(View.INVISIBLE);

Xml:

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

0

我们可以在Button xml中使用属性android:background,如下所示。

android:background="?android:attr/selectableItemBackground"

或者我们可以使用样式

style="?android:attr/borderlessButtonStyle" 为透明和阴影少的背景。


-2

transparent(light gray)单击按钮时,应用背景色。

ButtonName.setOnClickListener()

在上述方法中,您可以设置按钮的背景色。


6
担心这被否决了。请不要这样做。未来的维护者将诅咒您的名字。使用选择器设置背景,并按下一种颜色/可绘制,不按下一种。不要编写代码以在侦听器中更改它-这不是这样做的方式。
themightyjon 2015年

-2

您可以通过在xml文件中添加以下属性来轻松实现。此代码经过了大量时间的测试。

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

2
为什么要添加此答案?该解决方案已经包含在另外两个答案中,包括投票最多的一个。
Makyen
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.