android中colors.xml颜色设置资源文件的方法
1. 打开res目录下的values文件夹,双击打开colors.xml文件进行编辑
文章图片
上代码
#008577 #00574B #D81B60 #ff0000 #0000ff
2. 在res目录下的layout文件夹创建color_layout.xml文件

文章图片

文章图片
下述代码演示如何在XML文件中访问颜色.上代码
上述代码中@color/color_xml 为XML文件读取colors.xml文件中名为color_xml的RGB颜色代码,并将该颜色以 Text View的形式显示出来。3. 在java目录下的com.example.myapplication包中创建Color_ActivityDemo类

文章图片

文章图片
下述代码演示如何在Java代码中访问颜色。上代码
package com.example.myapplication; import android.os.Bundle; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class Color_ActivityDemo extends AppCompatActivity {TextView tv4; public void onCreate(Bundle saveInstanceState) {super.onCreate(saveInstanceState); setContentView(R.layout.color_layout); tv4 = (TextView) findViewById(R.id.tv4); tv4.setTextColor(getResources().getColor(R.color.color_java)); }}
上述代码中getResources ().getColor(R.color.color _java)为Java代码读取colors.xml文件中名为color_java的 RGB颜色代码,并将该颜色以TextView的形式显示出来。4. 在AndroidMainfest.xml文件添加Color_ActivityDemo.java的列表

文章图片

文章图片
5. 运行Color_ActivityDemo.java

文章图片
运行结果

文章图片
【android中colors.xml颜色设置资源文件的方法】到此这篇关于android中colors.xml颜色设置资源文件的文章就介绍到这了,更多相关android中colors.xml颜色内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- 10款Android和iPhone的最佳移动笔记应用合集
- Android和iPhone的8大最佳文档扫描仪应用合集
- 【外翻】Windows XP中的幽灵仍然困扰中小企业
- 电脑运用中必备的键盘快捷键组合技巧你知道多少?
- Android 开发笔记___FrameLayout
- Android Studio最新配置教程2016
- [android开发篇]安装android sdk的时候请注意
- Android上玩玩Hook()
- Android Studio使用手记
- Android 开发笔记___RelativeLayout