按钮背景是透明的

我有一个按钮。当我按下按钮时,我必须使文本加粗,否则正常。所以我为粗体&正常的。

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

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

315671 次浏览

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

ButtonName.setOnClickListener()

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

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

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

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

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

在你的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"/>

你还可以使用:

android:background="@null"

或者在代码中:

buttonVariable.setBackgroundColor(Color.TRANSPARENT);

尝试新的方法设置背景透明

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

我使用

btn.setBackgroundColor(Color.TRANSPARENT);

而且

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

代码:

button.setVisibility(View.INVISIBLE);

Xml:

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

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

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

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


第一个,为了使按钮背景透明,使用以下属性,因为这不会影响材质设计动画:

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

我是用XML实现的

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

您可以通过在xml文件中添加以下属性轻松地做到这一点。这段代码经过了大量的测试。

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

<强>步骤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"/>

我们可以在按钮 xml中使用属性android:背景,如下所示。

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

或者我们可以使用风格

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

我会说extend Borderless style。

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