Android按钮设计,自定义按钮,圆形按钮,颜色

本文详细介绍了如何在Android应用中自定义按钮,包括设置选择器以实现不同状态的行为,如禁用状态、不同可绘制对象,以及设置按钮的颜色、形状,如圆角和渐变效果。通过XML实现按钮的自定义设计,如设置背景颜色、文本颜色、边框和形状。
摘要由CSDN通过智能技术生成

In this tutorial, we’ll be customizing the Buttons in our Android Application. If you aren’t aware of Android Buttons, check out this tutorial before proceeding. We’ll be setting selectors and shapes on our buttons in xml.

在本教程中,我们将在Android应用程序中自定义按钮。 如果您不了解Android Button,请先阅读教程,然后再继续。 我们将在xml中的按钮上设置选择器和形状。

Android按钮设计 (Android Button Design)

A selector is used to define a different behaviour for different states of the Button.

选择器用于为按钮的不同状态定义不同的行为。

What are drawable states?

什么是可绘制状态?

Each of the following events of a view (Button or any other kind of view) are a type of state:

视图的以下每个事件(按钮或任何其他类型的视图)都是一种状态:

  • state_pressed

    state_pressed
  • state_selected

    state_selected
  • state_focused

    state_focused
  • state_enabled

    state_enabled

state_focused is when you hover over the widget. Typically works only on emulators.

当您将鼠标悬停在小部件上时,即为state_focused。 通常仅在模拟器上工作。

state_selected is meant to select the view. Doesn’t work with Buttons. Works with RadioButtons.

state_selected用于选择视图。 与按钮不兼容。 与RadioButtons一起使用。

Typically for a button, there are just three important states: normal, pressed and enabled.

通常,对于一个按钮,只有三个重要的状态:正常,按下和启用。

For each of the states of the selector, we can set a different drawable/color/shape on our button. Let’s get started with the implementation of each of these in a new Android Studio Project.

对于选择器的每种状态,我们可以在按钮上设置不同的drawable / color / shape。 让我们开始在一个新的Android Studio项目中实现这些功能。

Android自定义按钮项目结构 (Android Custom Button Project Structure)

We’ll be looking at each of the drawable files one at a time.

我们将一次查看每个可绘制文件。

The colors are defined in the colors.xml file :

颜色在colors.xml文件中定义:

A selector can be an xml file created inside the drawable folder.

android button selector

选择器可以是可绘制文件夹内创建的xml文件。

Selector for different background colors.
The following selector file btn_bg_selector.xml contains the code for setting different background colors on a button for different states.

不同背景颜色的选择器。
以下选择器文件btn_bg_selector.xml包含用于在按钮上为不同状态设置不同背景色的代码。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/red"/>
    <item android:state_focused="true" android:drawable="@color/yellow"/>
    <item android:drawable="@color/green"/>
</selector>

In the above code, each of the states is represented using item tag. The selector tag behaves like an if – else if statement to an extent. It checks every condition from top to bottom. Whenever the condition matches, it sets the relevant things on the button and stops processing the next item tags.

在上面的代码中,每个状态都使用item标签表示。 选择器标记在某种程度上类似于if – else if语句。 它从上到下检查每个条件。 只要条件匹配,它将在按钮上设置相关内容,并停止处理下一个项目标签。

The third item tag is the default one. It’s important that we keep it at the last. Keeping it at the top would not allow the other two item tags to get executed ever.

第三个标签是默认标签。 重要的是我们将其保留在最后。 将其保持在顶部将永远不会执行其他两个项目标签。

We set the above drawable selector file on our button in the activity_main.xml as:

我们在activity_main.xml的按钮上将上述可绘制选择器文件设置为:

<Button
            android:id="@+id/btnBgSelector"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值