开发者

android custom button shape

I am a beginner. So I request you to be patient with me. I am trying to achieve a custom shape for the button instead of the usual rectangular one. Is there any other way to do this than setting it as background for a button? Also i am trying to use different colours for different states for the button. Towards this I have created this file:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" 
          android:state_pressed="false" 
          android:drawable="@android:drawable/my_button_background_focus_blue" />
    <item android:state_focused="true" 
          android:state_pressed="true"
          android:drawable="@android:drawable/my_button_background_pressed_blue" />
    <item android:state_focused="false" 
          android:state_pressed="true"
   开发者_如何学JAVA   android:drawable="@android:drawable/my_button_background_pressed_blue" />
    <item android:drawable="@android:drawable/my_button_background_normal" />
</selector>

Do I include this inside the main.xml or create another xml file? If so, where do I create it? Thank you.


Is there any other way to do this than setting it as background for a button?

There are more ways, but this is the easier one. You could, for instance, create your own buttons implementing a custom View, but it does not worth the effort.

Do I include this inside the main.xml or create another xml file? If so, where do I create it?

You have to create a new XML file. You call it as you want, and place it in the res/drawable folder. Once you have done so, you can reference that file from XML or programatically:

<Button
    android:background="@drawable/the_name_of_the_xml"/>

Or...

button.setBackgroundResource(R.drawable.the_name_of_the_xml);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜