按钮背景透明

时间:2011-02-10 06:36:11

标签: android

我有一个按钮。当我按下按钮时,我必须将文本设为粗体,否则正常。所以我写了大胆的风格和正常。

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

如何在我的风格中包含透明背景?

14 个答案:

答案 0 :(得分:336)

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

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

仔细查看Android源中样式的使用方式。虽然他们不会在点击按钮时改变文本样式,但是有很多关于如何在那里实现目标的好主意。

答案 1 :(得分:99)

尝试新方法设置背景透明

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

答案 2 :(得分:35)

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

答案 3 :(得分:31)

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

答案 4 :(得分:28)

您也可以使用: 在你的xml中:

android:background="@null"

或代码:

buttonVariable.setBackgroundColor(Color.TRANSPARENT);

答案 5 :(得分:8)

我用XML实现了

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

答案 6 :(得分:7)

我用过

btn.setBackgroundColor(Color.TRANSPARENT);

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

答案 7 :(得分:4)

选择器仅适用于drawable,而不适用于样式。 Reference

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

style="?attr/buttonBarButtonStyle"

有很多方法可以设置按钮的样式。查看this tutorial

第二,要按下文本粗体,请使用以下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;
    }
});

答案 8 :(得分:2)

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

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

或者我们可以使用样式

style="?android:attr/borderlessButtonStyle"可实现透明且阴影较少的背景。

答案 9 :(得分:1)

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

颜色方案如下所示#AARRGGBB其中A代表alpha通道(透明度),R代表红色,G代表绿色,B代表蓝色。

答案 10 :(得分:1)

第1步: 在drawable和copy paste中创建一个新的资源文件

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

答案 11 :(得分:0)

单击按钮时,您将背景颜色应用为transparent(light gray)

ButtonName.setOnClickListener()

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

答案 12 :(得分:0)

代码:

button.setVisibility(View.INVISIBLE);

XML:

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

答案 13 :(得分:-1)

您可以通过在xml文件中添加以下属性来轻松完成。这段代码经过充分的时间测试。

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