Android颜色配置器配置方法
作者:小群子0618 发布时间:2022-06-10 23:39:33
一、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颜色配置器配置方法网站的支持!
来源:https://www.cnblogs.com/xqz0618/archive/2018/04/19/8882514.html


猜你喜欢
- 介绍Java中的建造者模式是一种创建型设计模式,它的主要目的是为了通过一系列简单的步骤构建复杂的对象,允许创建复杂对象的不同表示形式,同时隐
- 本文实例为大家分享了使用C#写一个时钟,供大家参考,具体内容如下时钟是这样的一共使用四个控件即可:WinFrom窗体应用程序代码:using
- 一、在idea中查看提交的历史记录右键单击项目单击git单击Show History结果展示:除了第一条记录是创建仓库默认就有的,
- 使用xml编写动态sql在Resources文件夹下创建一个Mapper文件夹比如我们需要在User表中使用增删改查,创建UserMappe
- 本文实例为大家分享了Unity实现攻击范围检测并绘制检测区域的具体代码,供大家参考,具体内容如下一、圆形检测using System.Col
- 前言在系统运行过程中,可能由于一些配置项的简单变动需要重新打包启停项目,这对于在运行中的项目会造成数据丢失,客户操作无响应等情况发生,针对这
- 在微服务中,需要我们在各个微服务中共享Session,使用Redis来共享Session是一个很好的解决方法,Redis是运行在内存中,查取
- 前言前面几篇我们学习的都是单表查询,就是对一张表中的数据进行查询。而实际项目中,基本都会有多张表联合查询的情况,今天我们就来了解下JPA的联
- 前言众所周知,微信聊天中我们输入一些关键词会有表情雨下落,比如输入「生日快乐」「么么哒」会有相应的蛋糕、亲吻的表情雨下落,今天就来完成这个表
- 记得上学的时候学习英语,每个英语老师说到英语翻译的时候都会说英语翻译要做到“信、达、雅”。如今做了一名程序员竟然体会我还是想用这三种境界来要
- 前言最近在学习Android开发,发现确实有太多东西需要去整理,去学习。慢慢来吧,任何东东的深入学习都是不简单的。今天稍微整理下Spanna
- Springboot添加server.servlet.context-pathserver.servlet.context-path配置的作
- 原理简介:zookeeper实现分布式锁的原理就是多个节点同时在一个指定的节点下面创建临时会话顺序节点,谁创建的节点序号最小,谁就获得了锁,
- 本文实例讲述了JDBC使用游标实现分页查询的方法。分享给大家供大家参考,具体如下:/*** 一次只从数据库中查询最大maxCount条记录*
- Overview在今天的开发学习中,我遇到了一个需求是在App的flash页面添加bing每日一图。这些都简单,但是当我获取到了图片的Url
- 介绍今天主要分享一下 kafka 的 rebalance,在 kafka 中,rebalance 是一个十分重要的概念,很多时候引发的一些问
- 默认情况下Spring Boot使用了内嵌的Tomcat服务器,项目最终被打成jar包运行,每个jar包可以被看作一个独立的Web服务器。传
- 前言本文将介绍通过Java编程在PDF文档中添加表格的方法。添加表格时,可设置表格边框、单元格对齐方式、单元格背景色、单元格合并、插入图片、
- 本文实例为大家分享了Dijkstra算法实现校园导游程序的具体代码,供大家参考,具体内容如下应用设计性实验1.问题描述校网导游程序: 一个校
- 图像切换器(ImageSwitcher),用于实现类似于windows操作系统下的windows照片查看器中的上一张 下一张切换图片的功能,