2015-11-01 58 views
3

我自定義的按鈕樣式'colorButtonNormal' 工作作爲按下狀態

<style name="custom_button" parent="@android:style/Widget.Button"> 
    <item name="android:paddingTop">5dp</item> 
    <item name="android:paddingBottom">5dp</item> 
    <item name="android:textAllCaps">false</item> 
    <item name="colorButtonNormal">#f37022</item> 
    <item name="android:textColor">#ffffff</item> 
</style> 

使用作爲

<Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Prepaid Package" 
     style="@style/custom_button"/>` 

但在colorButtonNormal顏色有出現在On press狀態。

現在該做什麼?

回答

2

我已經從這裏舉了個例子。

Custom background

如果您想自定義背景根據按鈕的狀態:

創建資源的XML文件如button_custom.xml /繪製/

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/button_pressed" 
     android:state_pressed="true" /> 
    <item android:drawable="@drawable/button_focused" 
     android:state_focused="true" /> 
    <item android:drawable="@drawable/button_default" /> 
</selector> 

然後實際的按鈕,添加:您可以將您的默認值與您的自定義背景合併。

<Button 
    ... 
    android:background="@drawable/button_custom" /> 

此外,在您的樣式,來修復您的自定義按鈕樣式的背景顏色:

<item name="android:background">#f37022</item> 

"colorButtonNormal"不是按鈕的屬性,你需要使用內置的屬性,如你有爲其他項目。

+0

'colorButtonNormal'應該是在'正常狀態'下工作,但它正在'按下'狀態下工作。 我知道答案的方式。但是我想在styles.xml中定義樣式 – Sudarshan