Inflating a view into Button?

谁说我不能喝 提交于 2019-12-08 09:48:41

问题


I saw this questions unanswered on some forums and wonder if anyone have a clue on how to answer.

Let's say for instance a button is needed with a picture and a text. It would seem natural to create an xml view and then inflate it into the Button. Is this possible?

At the moment I don't need the button to look clickable or change it's color at click, but it could also be interesting to consider how this will be affected by making the inflate.

Btw, I found this one tutorial describing a solution to the same problem, but they were writing a class extending View to obtain the goal. That seems a bit overklill to me. http://kahdev.wordpress.com/2008/09/13/making-a-custom-android-button-using-a-custom-view/

Thanks in advance! Anna


回答1:


Make a XML layout of your custom Button, that contains the TextView and ImageView. Put the following attributes to the root layout in the file:

android:background="@android:drawable/btn_default"
android:focusable="true"
android:clickable="true"

and you'll get what you need.



来源:https://stackoverflow.com/questions/3148713/inflating-a-view-into-button

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