android 自定义按钮形状

发布于 2024-10-09 11:57:27 字数 921 浏览 0 评论 0原文

我是初学者。所以我请求你对我有耐心。 我正在尝试为按钮实现一种自定义形状,而不是通常的矩形形状。 除了将其设置为按钮背景之外,还有其他方法可以做到这一点吗? 我还尝试对按钮的不同状态使用不同的颜色。为此,我创建了这个文件:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一紙繁鸢 2024-10-16 11:57:27

<块引用>

除了将其设置为按钮背景之外,还有其他方法可以做到这一点吗?

方法还有很多,但这是更简单的一种。例如,您可以创建自己的按钮来实现自定义视图,但这不值得付出努力。

<块引用>

我是否将其包含在 main.xml 中或创建另一个 xml 文件?如果是这样,我在哪里创建它?

您必须创建一个新的 XML 文件。您可以根据需要调用它,并将其放置在 res/drawable 文件夹中。完成此操作后,您可以从 XML 或以编程方式引用该文件:

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

或者...

button.setBackgroundResource(R.drawable.the_name_of_the_xml);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文