How to apply an style to all ImageButtons in Android?

给你一囗甜甜゛ 提交于 2019-12-06 08:06:45

问题


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.



来源:https://stackoverflow.com/questions/11380978/how-to-apply-an-style-to-all-imagebuttons-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!