android 自定义按钮形状
我是初学者。所以我请求你对我有耐心。 我正在尝试为按钮实现一种自定义形状,而不是通常的矩形形状。 除了将其设置为按钮背景之外,还有其他方法可以做到这一点吗? 我还尝试对按钮的不同状态使用不同的颜色。为此,我创建了这个文件:
<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"
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?如果是这样,我在哪里创建它? 谢谢。
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"
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
方法还有很多,但这是更简单的一种。例如,您可以创建自己的按钮来实现自定义视图,但这不值得付出努力。
您必须创建一个新的 XML 文件。您可以根据需要调用它,并将其放置在
res/drawable
文件夹中。完成此操作后,您可以从 XML 或以编程方式引用该文件:或者...
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.
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:Or...