Android资源访问——颜色资源

颜色资源
(1)直接在原始视图中修改背景颜色
打开activity_main.xml作一些修改:
主布局改为线性布局,垂直布局
使用原始视图 宽度fill_parent 高度50dp

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <View android:layout_width="fill_parent"
    android:layout_height="50dp"/>

</LinearLayout>

颜色值分为两种:rgb、argb。
a代表“阿尔法”通道
r代表红色通道
g代表绿色通道
b代表蓝色通道
每一个通道都用两位16进制数字来表示,以红色为例:
如果是#FFFF0000,前两位FF表示不透明,此为argb;
如果是#FF0000,含义为#FF(r)00(g)00(b)。

也可以使用简写,例如红色可以简写为f00,完全不透明的红色可以写成“#ff00”,带些透明的红色可以写成“#8f00”。

给activity_main.xml再写入一些代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <View android:background="#FFFF0000" android:layout_width="fill_parent" android:layout_height="50dp"/>
    <View android:background="#FF00FF00" android:layout_width="fill_parent" android:layout_height="50dp"/>
    <View android:background="#FF0000FF" android:layout_width="fill_parent" android:layout_height="50dp"/>
    <View android:background="#FFFF00FF" android:layout_width="fill_parent" android:layout_height="50dp"/>
</LinearLayout>

运行程序,打开app,出现下面的图片

从最终呈现的效果我们也了解了主布局为线性布局LinearLayout(从上往下),以及fill_parent的含义(填满)。

(2)把颜色提取出来单独定义
res->New->Android XML file->Resource Type:values/files:colors->完成
在colors.xml文件中写入

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#f00</color>  
    <color name="green">#0f0</color>
    <color name="blue">#00f</color>
</resources>

接下来就可以直接在activity_main.xml文件中直接使用这些定义的颜色

    <View android:background="@color/red" android:layout_width="fill_parent" android:layout_height="50dp"/>
    <View android:background="@color/green" android:layout_width="fill_parent" android:layout_height="50dp"/>
    <View android:background="@color/blue" android:layout_width="fill_parent" android:layout_height="50dp"/>
    <View android:background="#FFFF00FF" android:layout_width="fill_parent" android:layout_height="50dp"/>

效果一样。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值