2011-02-10 223 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"