一、Android Color设置

1、在xml文件中

想设置颜色直接设置background的属性或者其他的color属性。随便设置一个颜色如#000,再点击左边的颜色方块,弹出颜色选择器选择颜色

2、在java代码中

①Color.parseColor("#000");

  1. tvShow.setBackgroundColor(Color.parseColor("#000"));

【提示】可以在布局文件中配置好颜色值,然后把用“#”表示的颜色带到java代码中用

②Color.BLACK 使用Color类自带的颜色,不过都是一些基本色

  1. tvShow.setBackgroundColor(Color.BLACK);

③定义Color资源文件,通过R.color.myColor引用

  1. int color = R.color.myColor;
  1. tvShow.setBackgroundResource(R.color.myColor);

④Color.argb(a,r,g,b)方法:

  1. tvShow.setBackgroundColor(Color.argb(255, 255, 0, 0));

分别是alpha、红(red)、绿(green)、蓝(blue)四个颜色值(ARGB)。每个数字取值0-255,因此一个颜色可以用一个整数来表示。为了运行效率,Android编码时用整数Color类实例来表示颜色。

【提示】通过此方法传入对应的透明度值,红色值,绿色值,蓝色值进行颜色配置。因此我们可以通过此方法做一个简单的颜色配置器。

二、颜色配置器案例

1、【效果】

界面设计的比较粗糙,希望大家能学到实现效果,优化界面。

2、【项目结构】

3、【代码】

①activity_main.xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity"
  7. android:orientation="vertical">
  8.  
  9. <TextView
  10. android:text="请输入argb值:"
  11. android:textSize="20sp"
  12. android:textColor="#000"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:layout_margin="10dp"/>
  16.  
  17. <LinearLayout
  18. android:layout_width="match_parent"
  19. android:layout_height="50dp"
  20. android:orientation="horizontal">
  21. <EditText
  22. android:id="@+id/etA"
  23. android:layout_width="0dp"
  24. android:layout_weight="1"
  25. android:layout_height="match_parent"
  26. android:layout_margin="1dp"
  27. android:hint="透明度(0-255)"
  28. android:inputType="number"/>
  29.  
  30. <EditText
  31. android:id="@+id/etR"
  32. android:hint="红(0-255)"
  33. android:layout_width="0dp"
  34. android:layout_weight="1"
  35. android:layout_height="match_parent"
  36. android:background="#f00"
  37. android:layout_margin="1dp"
  38. android:gravity="center"
  39. android:inputType="number"/>
  40. </LinearLayout>
  41. <LinearLayout
  42. android:layout_width="match_parent"
  43. android:layout_height="50dp"
  44. android:orientation="horizontal">
  45. <EditText
  46. android:id="@+id/etG"
  47. android:hint="绿(0-255)"
  48. android:layout_width="0dp"
  49. android:layout_weight="1"
  50. android:layout_height="match_parent"
  51. android:background="#0f0"
  52. android:layout_margin="1dp"
  53. android:gravity="center"
  54. android:inputType="number"/>
  55.  
  56. <EditText
  57. android:id="@+id/etB"
  58. android:hint="蓝(0-255)"
  59. android:layout_width="0dp"
  60. android:layout_weight="1"
  61. android:layout_height="match_parent"
  62. android:background="#00f"
  63. android:layout_margin="1dp"
  64. android:gravity="center"
  65. android:inputType="number"/>
  66. </LinearLayout>
  67.  
  68. <TextView
  69. android:id="@+id/tv_show"
  70. android:text="TextView"
  71. android:layout_width="200dp"
  72. android:layout_height="200dp"
  73. android:background="#000"
  74. android:layout_gravity="center"
  75. android:layout_marginTop="20dp"
  76. />
  77.  
  78. <Button
  79. android:id="@+id/btn"
  80. android:text="确定配置"
  81. android:layout_margin="20dp"
  82. android:layout_width="match_parent"
  83. android:layout_height="wrap_content" />
  84. </LinearLayout>

【提示】EditText 中hint属性:这是设置输入框内的提示文字。 inputType属性:设置输入框输入的文本类型,此处设置为整数型

②MainActivity.java文件

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2.  
  3. private EditText etA;
  4. private EditText etR;
  5. private EditText etG;
  6. private EditText etB;
  7. private TextView tvShow;
  8. private Button btn;
  9.  
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14.  
  15. initView();
  16. }
  17.  
  18. private void initView() {
  19. etA = (EditText) findViewById(R.id.etA);
  20. etR = (EditText) findViewById(R.id.etR);
  21. etG = (EditText) findViewById(R.id.etG);
  22. etB = (EditText) findViewById(R.id.etB);
  23. tvShow = (TextView) findViewById(R.id.tv_show);
  24. btn = (Button) findViewById(R.id.btn);
  25.  
  26. btn.setOnClickListener(this);
  27. }
  28.  
  29. @Override
  30. public void onClick(View v) {
  31. switch (v.getId()) {
  32. case R.id.btn:
  33. submit();
  34. break;
  35. }
  36. }
  37.  
  38. private void submit() {
  39. // validate
  40. if (!etA.getText().equals("")&&!etB.getText().equals("")
  41. &&!etR.getText().equals("")&&!etG.getText().equals("")) {
  42. //对用户输入的数值进行判断是否为空。避免空字符无法转换为int异常
  43. int et_a = Integer.parseInt(etA.getText().toString());
  44. int et_r = Integer.parseInt(etR.getText().toString());
  45. int et_g = Integer.parseInt(etG.getText().toString());
  46. int et_b = Integer.parseInt(etB.getText().toString());
  47. tvShow.setBackgroundColor(Color.argb(et_a, et_r, et_g, et_b));
  48. }else {
  49. Toast.makeText(this, "输入的值不能为空", Toast.LENGTH_SHORT).show();
  50. }
  51. }
  52. }

Android颜色配置器的更多相关文章

  1. Android 颜色配置表-颜色类

    android开发中,常常会用到color.xml颜色配置,好的颜色配置可以让尼的应用让人看起来赏心悦目! 不罗嗦,上图先 该工程已经罗列了常用的颜色配置 附上工程链接:http://download ...

  2. 实用的android颜色配置表(亮瞎尼的双眼)

    android开发中,常常会用到color.xml颜色配置,好的颜色配置可以让尼的应用让人看起来赏心悦目! 不罗嗦,上图先 该工程已经罗列了常用的颜色配置 附上工程链接:http://download ...

  3. 【转】android颜色对应的xml配置值

    原文网址:http://www.cnblogs.com/etgyd/archive/2011/04/02/2003778.html android颜色对应的xml配置值 <?xml versio ...

  4. 基于Android的rgb七彩环颜色采集器

    代码地址如下:http://www.demodashi.com/demo/11892.html 一.前言. 在大学期间,看到这个rgb灯,蛮好奇的,这么漂亮的颜色采集,并且可以同步到设备rbg灯颜色, ...

  5. Android Studio 配置SVN实现代码管理

    Refference From:http://iaiai.iteye.com/blog/2267346 一.Android Studio配置SVN Android Studio关联配置SVN很简单,在 ...

  6. android 资讯阅读器

    最近找申请到了一个不错的接口 , 非常适合拿来写一个资讯类的app. 现在着手写,随写随更.也算是抛砖引玉.烂尾请勿喷.╭(╯^╰)╮ android 资讯阅读器 第一阶段目标样式(滑动切换标签 , ...

  7. Android 颜色渲染(六) RadialGradient 环形渲染

    Android 颜色处理(六) RadialGradient 环形渲染 public RadialGradient(float x, float y, float radius, int[] colo ...

  8. Android 颜色渲染(十) ComposeShader组合渲染

    版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] Android 颜色处理(十) ComposeShader组合渲染 public ComposeShader(Shader sh ...

  9. Android 颜色渲染(五) LinearGradient线性渲染

    版权声明:本文为博主原创文章,未经博主允许不得转载. Android 颜色处理(五) LinearGradient线性渲染 相信很多人都看过歌词同步的效果, 一是竖直方向的滚动,另一方面是水平方面的歌 ...

随机推荐

  1. guava-19.0和google-collections-1.0 的 ImmutableSet 类冲突

    guava-19.0 google-collections-1.0 都有 ImmutableSet 类,包路径也一致,前者有 copyOf(Collection)? 一.应用报错: 二.解决办法 co ...

  2. virtualbox中linux系统与windows实现共享文件夹

    最近有一次,需要在linux获取在我windows系统里的安装包,但是呢不论如何也拿不过去. virtualbox虽然提供了双向拖放,但是实在是太不健壮了,感觉基本就没好使过. 于是我想到了用共享文件 ...

  3. 数十种TensorFlow实现案例汇集:代码+笔记(转)

    转:https://www.jiqizhixin.com/articles/30dc6dd9-39cd-406b-9f9e-041f5cbf1d14 这是使用 TensorFlow 实现流行的机器学习 ...

  4. p2p项目总结

    1.关于ajax请求所要注意的地方:$.psot(url,json,callback,type) (1)url路径问题,在html中写绝对路径不能用EL表达式,EL表达式只能在jsp中使用 (2)js ...

  5. [LeetCode] Freedom Trail 自由之路

    In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal ...

  6. Python模块之 - logging

    日志是非常重要的,最近有接触到这个,所以系统的看一下Python这个模块的用法.本文即为Logging模块的用法简介,主要参考文章为Python官方文档,链接见参考列表. Logging模块构成 组成 ...

  7. Mysql之库表操作(胖胖老师)

    SQL概念:结构化查询语言(SQL = Structured Query Language),也是一种编程语言(数据库查询和程序设计语言),可以用于数据的存取及查询,更新,管理关系型数据库系统ps: ...

  8. Redis常用命令--Hashes

    Hash是由键值对组成的map.Hashes的底层是通过字典实现的.一个哈希表里面可以有多个哈希表节点.而每个哈希节点就保存了字典中的一个键值对. 字典是一种用于保存键和值对的抽象数据结构.字典里的每 ...

  9. cogs 619. [金陵中学2007] 传话

    提交地址:http://cojs.tk/cogs/problem/problem.php?pid=619 619. [金陵中学2007] 传话 ★☆   输入文件:messagez.in   输出文件 ...

  10. [Codeforces]856C - Eleventh Birthday

    题目大意:给出n个数,问有多少种排列把数字接起来是11的倍数.(n<=2000) 做法:一个数后面接一个数等同于乘上10的若干次幂然后加上这个数,10模11等于-1,所以10的若干次幂是-1或1 ...