2011-02-10 225 views
124

我有一个按钮。当我按下按钮时,我必须使文本为粗体,否则正常。所以我写了大胆的&正常的样式。按钮背景为透明

<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-08-30 01:11:29

+0

退房[这个答案在这里(http://stackoverflow.com/questions/12856661/how-to-make-button-transparent -in-my-app-yet-visible#answer-12856901)制作具有边框的透明按钮 – 2016-03-31 22:10:02

+0

'为什么不使用TextView并在onClick上调用动作? – 2016-05-07 13:05:32

回答

0

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

ButtonName.setOnClickListener() 

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

+4

担心这是upvoted。请不要这样做。未来的维护者会诅咒你的名字。使用选择器设置背景,并使用一种颜色/可拖动按钮,一种不可用。不要在代码中编写代码来改变它 - 这不是这样做的。 – themightyjon 2015-07-17 11:25:55

295

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

但是,您的问题似乎有点深刻,因为您以非常奇怪的方式使用选择器。你使用它的方式似乎是错误的,但如果它真的有效,你应该把背景图像作为<item/>

仔细看看Android源代码中如何使用样式。虽然他们不会通过点击按钮来改变文本样式,但如何在那里实现您的目标还有很多好的想法。

+0

我已经给android:背景作为XML .. – Mathew 2011-02-10 06:59:17

+1

看看我澄清的答案。我误解你的问题开始。背景被设置为风格。 – 2011-02-10 07:03:54

33

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

28

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

你也可以使用: 在你的XML:

android:background="@null" 

或代码:

buttonVariable.setBackgroundColor(Color.TRANSPARENT); 
66

尝试新方式设置背景透明

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

我用

btn.setBackgroundColor(Color.TRANSPARENT); 

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

代码:

button.setVisibility(View.INVISIBLE); 

XML:

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

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

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

1

选择器仅适用于drawables,不适用于样式。 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; 
    } 
}); 
2

我在XML与Android实现这一点:背景= “@安卓:彩色/透明”

0

您可以通过在xml文件中添加以下属性轻松完成此操作。这段代码经过了大量的测试。

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