一、Android Color设置

1、在xml文件中

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

2、在java代码中

①Color.parseColor("#000");

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

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

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

tvShow.setBackgroundColor(Color.BLACK);

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

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

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

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

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

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

二、颜色配置器案例

1、【效果】

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

2、【项目结构】

3、【代码】

①activity_main.xml布局文件

 <?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity"
android:orientation="vertical"> <TextView
android:text="请输入argb值:"
android:textSize="20sp"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"/> <LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<EditText
android:id="@+id/etA"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:hint="透明度(0-255)"
android:inputType="number"/> <EditText
android:id="@+id/etR"
android:hint="红(0-255)"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#f00"
android:layout_margin="1dp"
android:gravity="center"
android:inputType="number"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<EditText
android:id="@+id/etG"
android:hint="绿(0-255)"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#0f0"
android:layout_margin="1dp"
android:gravity="center"
android:inputType="number"/> <EditText
android:id="@+id/etB"
android:hint="蓝(0-255)"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#00f"
android:layout_margin="1dp"
android:gravity="center"
android:inputType="number"/>
</LinearLayout> <TextView
android:id="@+id/tv_show"
android:text="TextView"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#000"
android:layout_gravity="center"
android:layout_marginTop="20dp"
/> <Button
android:id="@+id/btn"
android:text="确定配置"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

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

②MainActivity.java文件

 public class MainActivity extends AppCompatActivity implements View.OnClickListener {

     private EditText etA;
private EditText etR;
private EditText etG;
private EditText etB;
private TextView tvShow;
private Button btn; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initView();
} private void initView() {
etA = (EditText) findViewById(R.id.etA);
etR = (EditText) findViewById(R.id.etR);
etG = (EditText) findViewById(R.id.etG);
etB = (EditText) findViewById(R.id.etB);
tvShow = (TextView) findViewById(R.id.tv_show);
btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
submit();
break;
}
} private void submit() {
// validate
if (!etA.getText().equals("")&&!etB.getText().equals("")
&&!etR.getText().equals("")&&!etG.getText().equals("")) {
//对用户输入的数值进行判断是否为空。避免空字符无法转换为int异常
int et_a = Integer.parseInt(etA.getText().toString());
int et_r = Integer.parseInt(etR.getText().toString());
int et_g = Integer.parseInt(etG.getText().toString());
int et_b = Integer.parseInt(etB.getText().toString());
tvShow.setBackgroundColor(Color.argb(et_a, et_r, et_g, et_b));
}else {
Toast.makeText(this, "输入的值不能为空", Toast.LENGTH_SHORT).show();
}
}
}

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. Mac里安装Jmeter

    前提是需要安装jdk,参见http://www.cnblogs.com/fun0623/p/4703456.html 1.解压包 (双击apache-jmeter-2.13) 2.进去到解压后的bin ...

  2. (第一章)对程序员来说CPU是什么

    这几天,看到一本书,<程序是怎么跑起来的>,觉得之前都没有完整的看完一本书,现在要从这本书开始,慢慢的培养自己写读书笔记的习惯,不能度过去就忘了. 学习是一个螺旋上升的过程,不要指望一下子 ...

  3. python 杂货铺

    python 杂货铺之不知道的python操作 1.交互模式下的神奇的_ windos中cmd交互模式中下(python2,python3),最近一个表达式的值赋给变量 _.这样我们就可以把它当作一个 ...

  4. POJ-3026 Borg Maze---BFS预处理+最小生成树

    题目链接: https://vjudge.net/problem/POJ-3026 题目大意: 在一个y行 x列的迷宫中,有可行走的通路空格' ',不可行走的墙'#',还有两种英文字母A和S,现在从S ...

  5. ORACLE数据库编程

    第一章 Oracle数据库基本概念 一.介绍 Oracle数据库系统是美国Oracle(甲骨文)公司提供的以分布式数据库为 核心的一组软件产品,是目前最流行的客户/服务器(Client/Server, ...

  6. z-index的学习整理转述

    前言:这是笔者第一次写博客,主要是学习之后自己的理解.如果有错误或者疑问的地方,请大家指正,我会持续更新! z-index属性描述元素的堆叠顺序(层级),意思是A元素可以覆盖B元素,但是B元素并没有消 ...

  7. [LeetCode] Longest Univalue Path 最长相同值路径

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

  8. java面试2(java技术栈和Hollis面试内容分享)

    1.什么是java虚拟机? java虚拟机(JVM)是一个可执行java字节码的虚拟机进程,java源文件被编译成能被java虚拟机可执行的字节码文件. 2.什么是平台无关性,java是如何做到平台无 ...

  9. PyQT5 helloworld教程(转载)

    转载节选自该博客地址:http://blog.csdn.net/u013401853/article/details/54581512,博主的步骤写的很详细,感谢! QT Creator安装 因为我们 ...

  10. 用js来实现那些数据结构09(集合01-集合的实现)

    说到集合,第一个想到的就是中学学到的那个数学概念:集合.在我们开始集合相关的js实现前,我们有必要来了解一下什么是集合以及集合的数学概念. 好吧,我们一起来复习一下早就被我们遗忘的集合. 集合是由一组 ...