我有一个按钮。当我按下按钮时,我必须使文本加粗,否则正常。所以我写了粗体和正常样式。

<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 as:

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

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


当前回答

我会说扩展无界风格。

<style name="Button" parent="Widget.AppCompat.Button.Borderless">
    <item name="android:textColor">@color/blue</item>
    <item name="android:textAllCaps">true</item>
</style>

其他回答

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

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

我使用

btn.setBackgroundColor(Color.TRANSPARENT);

and

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

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

我是用XML实现的

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

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