Android之Button

本文介绍如何在Android项目中创建和定制Button组件,包括通过XML布局文件直接设置文本和颜色,以及利用strings.xml和colors.xml文件进行资源引用的方法。
1.新建android项目,打开activity_main.xml文件,选择下方Design,如果选中Button,在右侧android设备中添加Button
在Text中可以看到Button的参数设置,可以更改android:text的值修改Button中的显示字符
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView"
    android:layout_marginTop="40dp"
    android:layout_toEndOf="@+id/textView"
    android:text="@string/btn_text"
    android:background="@color/colorPrimary"/>
2.也可以在strings.xml文件中添加资源信息
<resources>
<stringname="app_name">MyTest160707</string>
<stringname="btn_text">倔强的赞叹2</string>
</resources>
然后将android:text的值更改为btn_text的赋值
android:text="@string/btn_text"
这时显示的Button的字符就是“倔强的赞叹2”
3.同理可以在colors.xml文件中设置颜色属性,在activity_main.xml文件中使用
<?xml version="1.0"encoding="utf-8"?>
<resources>
<colorname="colorPrimary">#3F51B5</color>
<colorname="colorPrimaryDark">#303F9F</color>
<colorname="colorAccent">#FF4081</color>
</resources>
调用
android:background="@color/colorPrimary"
在Text中可以看到Button的参数设置,可以更改android:text的值修改Button中的显示字符
2.也可以在strings.xml文件中添加资源信息
<resources>
<stringname="app_name">MyTest160707</string>
<stringname="btn_text">倔强的赞叹2</string>
</resources>
然后将android:text的值更改为btn_text的赋值
android:text="@string/btn_text"
这时显示的Button的字符就是“倔强的赞叹2”
3.同理可以在colors.xml文件中设置颜色属性,在activity_main.xml文件中使用
<?xml version="1.0"encoding="utf-8"?>
<resources>
<colorname="colorPrimary">#3F51B5</color>
<colorname="colorPrimaryDark">#303F9F</color>
<colorname="colorAccent">#FF4081</color>
</resources>
调用
android:background="@color/colorPrimary"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值