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

*爱你&永不变心* 提交于 2021-01-28 07:02:50

问题


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.

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

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


回答1:


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

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

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"



回答2:


Try this here and another

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



回答3:


I think this is what you're looking for.

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


来源:https://stackoverflow.com/questions/46099548/how-to-create-borderless-button-using-style-from-style-xml-file-along-with-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!