Today we’ll dive deep into Android Buttons in Material Design and develop an application that showcases the different styles of a Button.
今天,我们将深入研究Material Design中的Android Buttons,并开发一个应用程序来展示Button的不同样式。
Android Material Design按钮 ()
Buttons in Android are used to communicate our actions with the application. The introduction of Material Design has brought along many different kinds of Button styles which are compatible with pre-Lollipop devices too.
Android中的按钮用于与应用程序交流我们的操作。 Material Design的引入带来了许多不同类型的Button样式,它们也与之前的Lollipop设备兼容。
A basic Button in the layout is defined like this:
布局中的基本Button定义如下:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context="com.journaldev.androidbuttonstyling.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NORMAL BUTTON"/>
</LinearLayout>
The above Button has the default style: Widget.AppCompat.Button
上面的Button具有默认样式: Widget.AppCompat.Button
To set a different style on the Button, we use the android:background
attribute.
要在Button上设置其他样式,我们使用android:background
属性。
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:textColor="@android:color/white"
android:layout_margin="12dp"
android:text="BACKGROUND COLOR BUTTON"/>
The output of both of the above Buttons looks like this: