Android removes button (Button) background and border and makes button background transparent [Create borderless button]

Preface

Buttons in Android are the most commonly used controls, so there are many questions about modifying button styles on the Internet. In custom styles, a common need is to remove button background. For example, in the picture below:
Two buttons with transparent background
the button icon on the left can use ImageButtonor ImageViewto load an icon with a transparent background. The button on the right that only displays text can only use normal Button.

For the button on the right, the ways to achieve the UI effect above are:

  • Set the button background and border to the same color as the bottom to achieve visual unity.
  • Use TextViewcontrols as buttons. TextViewThe default is a text box with a transparent background and click events available.

The first solution cannot adapt to complex background situations. For example, if the background is wallpaper, Buttonthe background will be exposed.

Although the second option is feasible, it is not elegant enough. Because TextViewthere is no button click effect, such as water ripples, color changes after pressing, etc., if you want to achieve the above functions, you need more cumbersome style customization.



text

In fact, Google officially provides a button style without background: style="?android:attr/borderlessButtonStyle", called borderless button . After using this style, you can create a button without background and border, but its appearance will still change depending on the state (such as after clicking). The Cancel button in the picture above is a typical application scenario.

<Button
	style="?android:attr/borderlessButtonStyle"
	android:id="@+id/button"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="Button" />

Similar effects include:

  • style="?android:attr/buttonBarStyle"
  • style="?android:attr/buttonBarButtonStyle"

Everyone can try it. Although they are all transparent and borderless, the effects they bring are slightly different. You can choose the most suitable one.



end

For more information, please view: Android Development Manual - Borderless Buttons

Guess you like

Origin blog.csdn.net/Guan_li_peng/article/details/129423617