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

时间:2017-09-07 14:57:46

标签: android xml android-layout android-button

我正在尝试创建一个无边框按钮,但我的按钮还有许多其他样式,我想通过将代码嵌入到我的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中实现这一点,我不知道' _______ 采用了什么值?

3 个答案:

答案 0 :(得分:2)

你可以通过制作扩展默认Button类的Custom Button类来实现这一点,并在任何地方使用这个类,并使用第二种可能的解决方案

如果选择或未选中,则应使用切换按钮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"

答案 1 :(得分:0)

试试这个hereanother

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

答案 2 :(得分:-1)

我认为这就是您想要的。

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