如何使用 style.xml 文件中的样式(以及所有其他样式)创建无边框按钮

How to create borderless button using style from style.xml file (along with all other styles)

我正在尝试创建一个无边框按钮,但我的按钮还有许多其他样式,我想通过将代码嵌入到我的 style.xml 文件中来设计无边框按钮。

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

<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 中做到这一点,但我不知道 '_______ 取什么值?

试试这个here and another

android:background="?android:attr/selectableItemBackground"

您可以通过制作扩展默认按钮 class 的自定义按钮 class 并在任何地方使用此 class 来实现此目的,第二种可能的解决方案是使用

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

请注意还有 4 个状态

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

<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>

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

android:background="@drawable/like_button"

我想这就是您要找的。

<style name="style_name" parent="@style/Widget.AppCompat.Button.Borderless">
    <!-- your style -->
</style>