2011-12-08 7 views
6

Tôi đã làm theo hướng dẫn trên creating custom buttons. Nếu tôi áp dụng phong cách cho nút trực tiếp nó hoạt động, nhưng nếu tôi áp dụng phong cách cho chủ đề bằng cách sử dụng android:buttonStyle nó không hoạt động.Nút áp dụngPhong cách làm chủ đề không hoạt động nhưng nếu áp dụng cho nút trực tiếp

Một ví dụ - Đây là xml phong cách với kiểu nút áp dụng: giá trị/styles.xml

<resources> 
    <style name="MyTheme" parent="android:Theme.Light"> 
     <!-- the button gets styled but is no longer clickable 
      if i do it like this --> 
     <item name="android:buttonStyle">@style/ButtonText</item> 
    </style> 

    <style name="ButtonText"> 
     <item name="android:layout_width">fill_parent</item> 
     <item name="android:layout_height">wrap_content</item> 
     <item name="android:textColor">#ffffff</item> 
     <item name="android:background">@drawable/ruddy_orange_button</item> 
    </style> 
</resources> 

drawable/ruddy_orange_button.xml:

<item android:state_pressed="true"> 
     <shape> 
      <solid android:color="#70c656" /> 
      <stroke android:width="1dp" android:color="#53933f" /> 
      <corners android:radius="3dp" /> 
      <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" /> 
     </shape> 
    </item> 
    <item> 
     <shape> 
      <gradient android:angle="270" android:endColor="#fd4d4d" android:startColor="#f24b4b" /> 
      <stroke android:width="1dp" android:color="#f04a4a" /> 
      <corners android:radius="4dp" /> 
      <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" /> 
     </shape> 
    </item> 
</selector> 

Tuy nhiên nếu tôi áp dụng phong cách trực tiếp đến nút hoạt động:

<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    style="@style/ButtonText" 
    android:text="hello there" /> 

Bất kỳ con trỏ nào về những gì tôi đang làm sai ở đây?

Tôi đã thử nghiệm điều này trên Android 2.3.3

Nguồn là trên github.

Trả lời

9

Hãy thử điều này:

<style name="ButtonText" parent="@android:style/Widget.Button"> 
6

Tôi cũng đã problem.hope này mũi sẽ giúp bạn

appcompat v21 + bạn nên sử dụng buttonStyle thay vì android:buttonStyle trong chủ đề của bạn