如何使用style.xml文件中的样式(以及所有其他样式)创建无边界按钮 [英] How to create borderless button using style from style.xml file (along with all other styles)

查看:143
本文介绍了如何使用style.xml文件中的样式(以及所有其他样式)创建无边界按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建无边界按钮,但是我的按钮也有许多其他样式,并且我想通过将代码嵌入到我的style.xml文件中来设计无边界按钮.

I am trying to create a borderless button, but I also have many other styles for my button and I want to design button borderless by embedding code into my style.xml file.

我发现的一种方法是:通过在我的布局文件中使用style =?android:attr/borderlessButtonStyle".

One way I found was: By using style="?android:attr/borderlessButtonStyle" in my layout file.

<Button
    android:id="@+id/button_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage"
    style="?android:attr/borderlessButtonStyle" />

但是我想在style.xml中做到这一点,我不知道' _______ 取什么值?

But I want to make this happen in style.xml and I don't know what values does ' _______ take?

推荐答案

您可以通过使Custom Button类扩展默认的Button类,并在所有地方使用该类来实现此目的,第二种可能的解决方法是使用

You can do this by making Custom Button class which extend default Button class, and use this class everywhere, and second possible solution is use

如果已选中或未选中它,则应使用切换按钮 https://developer.android.com/reference/android/widget/ToggleButton.html

If it's selected or not selected you should use a toggle button https://developer.android.com/reference/android/widget/ToggleButton.html

请注意,该状态仍然有4种状态

Be aware that there are still 4 states for that

您可以在这样的选择器中定义它们

You define them in a selector like this

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/likeactivepressed" />
    <item android:state_pressed="true" android:drawable="@drawable/likeinitialpressed"/>
    <item android:state_checked="true" android:drawable="@drawable/likeon"/>
    <item android:drawable="@drawable/likeinitial"/>
</selector>

然后像这样在您的按钮中定义它

Then define it in your button like this

android:background="@drawable/like_button"

这篇关于如何使用style.xml文件中的样式(以及所有其他样式)创建无边界按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆