How to apply an style to all ImageButtons in Andro

2020-07-23 05:23发布

问题:

I have the following style in my application.

  <style name="BtnPressed">
      <item name="android:background">@drawable/btn_default</item>
  </style>

Now I want to apply that to all the ImageButtons in my application. I tried to use the solution mentioned in this question but it did not work. I'm using API-7.

回答1:

Change your BtnPressed style to this:

<style name="BtnPressedStyle" parent="Widget.ImageButton">
  <item name="android:background">@drawable/btn_default</item>
</style>

Define an application-wide theme/style:

<style name="MyTheme" parent="android:Theme">
   <item name="android:imageButtonStyle">@style/BtnPressedStyle</item>
</style>

Then apply it to your application tag of the manifest:

<application theme="@style/MyTheme" ... />

Let me know if this works, I adapted it from my code that overrides application-wide button style, but ImageButton should work just as well.



回答2:

You can also spend about 5 minutes in your IDE (notepad++ will do) and search and replace ImageView on xmls , and a little snippet for image views generated in code. I don't think it's so wrong, and will help you to maintain the app.