版权声明:本文为博主原创文章,未经博主允许不得转载。

前言

在Android开发中,经常需要设置控件的背景颜色或者图片的src颜色。

效果图

代码分析

根据使用的方法不同,划分为

  • setBackgroundColor方法【一般用于RelativeLayout、TextView等控件】
  1. 使用colors.xml文件中的颜色
  2. 使用颜色的int类型值
  3. 使用颜色的16进制类型值
  • setImageDrawable方法【一般用于ImageView控件】
  1. 使用colors.xml文件中的颜色
  2. 使用颜色的int类型值
  3. 使用颜色的16进制类型值
//setBackgroundColor方法
mLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent));//使用colors.xml文件中的颜色
mTvColorInt.setBackgroundColor(new Integer(-12590395));//使用颜色的int类型值
mTvColorHex.setBackgroundColor(Color.parseColor("#3FE2C5"));//使用颜色的16进制类型值 //setImageDrawable方法
Drawable drawableColor1 = new ColorDrawable(ContextCompat.getColor(this, R.color.colorAccent));//使用colors.xml文件中的颜色【在这里未使用,只是用来说明一种方式】
Drawable drawableColor2 = new ColorDrawable(new Integer(-2132153879));//使用颜色的int类型值【在这里未使用,只是用来说明一种方式】
Drawable drawableColor = new ColorDrawable(Color.parseColor("#80e9e9e9"));//使用颜色的16进制类型值 mImgColor.setImageDrawable(drawableColor);//设置ImageView控件的src属性值

使用步骤

activity_main.xml布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:id="@+id/tv_colorint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="使用-12590395的颜色值"
android:layout_centerInParent="true"/> <TextView
android:id="@+id/tv_colorhex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="使用#3FE2C5的颜色值"
android:layout_below="@id/tv_colorint"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"/> <ImageView
android:id="@+id/img_color"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#ffffff"
android:background="@mipmap/ic_launcher"
android:contentDescription="@string/app_name"
android:layout_below="@id/tv_colorhex"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
/>
</RelativeLayout>

MainActivity.java文件如下:

package com.why.project.colorutildemo;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private RelativeLayout mLayout;
private TextView mTvColorInt;
private TextView mTvColorHex;
private ImageView mImgColor; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initViews();
initData();
} private void initViews(){
mLayout = (RelativeLayout) findViewById(R.id.activity_main);
mTvColorInt = (TextView) findViewById(R.id.tv_colorint);
mTvColorHex = (TextView) findViewById(R.id.tv_colorhex);
mImgColor = (ImageView) findViewById(R.id.img_color);
} private void initData() { //setBackgroundColor方法
mLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent));//使用colors.xml文件中的颜色
mTvColorInt.setBackgroundColor(new Integer(-12590395));//使用颜色的int类型值
mTvColorHex.setBackgroundColor(Color.parseColor("#3FE2C5"));//使用颜色的16进制类型值 //setImageDrawable方法
Drawable drawableColor1 = new ColorDrawable(ContextCompat.getColor(this, R.color.colorAccent));//使用colors.xml文件中的颜色【在这里未使用,只是用来说明一种方式】
Drawable drawableColor2 = new ColorDrawable(new Integer(-2132153879));//使用颜色的int类型值【在这里未使用,只是用来说明一种方式】
Drawable drawableColor = new ColorDrawable(Color.parseColor("#80e9e9e9"));//使用颜色的16进制类型值
mImgColor.setImageDrawable(drawableColor);//设置ImageView控件的src属性值 }
}

Android中设置控件的背景颜色的方式整理的更多相关文章

  1. android中设置控件获得焦点 (转)

    android中,要使控件获得焦点,需要先setFocus,再requestFocus. 以Button为例:                 btn.setFocusable(true);      ...

  2. android中设置控件获得焦点

    android中,要使控件获得焦点,需要先setFocus,再requestFocus. 以Button为例:                btn.setFocusable(true);       ...

  3. VC、MFC中设置控件的背景色、标题、字体颜色、字体要注意的地方[转]

    在MFC中设置控件的背景色.字体.字体颜色.标题等属性主要是利用OnCtlColor函数来实现. 如: HBRUSH CAlarm::OnCtlColor(CDC* pDC, CWnd* pWnd, ...

  4. MFC中给各个控件填充背景颜色的方法

    1.给程序设置大背景色,在OnPaint()函数中添加如下代码: CRect rect; CPaintDC dc(this); GetClientRect(rect); dc.FillSolidRec ...

  5. Android线程中设置控件

    在Android中经常出现多线程中设置控件的值报错的情况,今天教大家封装一个简单的类避免这样的问题,同样也调用实现也非常的方便. 自定义类: /** * Created by wade on 2016 ...

  6. android中在java代码中设置Button按钮的背景颜色

    android中在java代码中设置Button按钮的背景颜色 1.设置背景图片,图片来源于drawable: flightInfoPanel.setBackgroundDrawable(getRes ...

  7. Android在代码中设置控件的drawableLeft,drawableRight,drawableTop,drawableBottom。

    根据业务的需要,要在代码中设置控件的drawableLeft,drawableRight,drawableTop,drawableBottom属性. 我们知道在xml中设置的方法为:android:d ...

  8. Android 中常见控件的介绍和使用

    1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...

  9. Android中ExpandableListView控件基本使用

    本文採用一个Demo来展示Android中ExpandableListView控件的使用,如怎样在组/子ListView中绑定数据源.直接上代码例如以下: 程序结构图: layout文件夹下的 mai ...

随机推荐

  1. MongoDB学习记录(二) - MongoDB的"增查改删"操作之"增"

    如果做插入文档操作的集合不存在,那么集合将被创建 db.collection.insertOne() insertOne为collection插入一条文档,如果文档的_id字段未指定,MongoDB会 ...

  2. arm 执行 交叉编译完成的可执行文件时 出现premission denied 问题

    我用的tftp传过去的文件 然后传完 执行的时候 出现了premission denied (权限不够)得问题 解决方法 就是添加权限  chmod 777 filename

  3. JavaScript-BOM与DOM

    BOM与DOM BOM: Browser Object Model(浏览器对象模型),即把 浏览器 当做一个对象来看待.BOM 除了可以访问文档中的组件之外,还可以访问 浏览器组件,比如页面中的 na ...

  4. HDU 6377 度度熊看球赛 (计数DP)

    度度熊看球赛 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  5. 关于字符的C++函数

    toupper(), tolower()不会改变原来的字符; 如果输入不是字母, 返回值跟原字符相同. isupper(), islower()..

  6. sqoop错误集锦1

    1.当时初学Sqoop的时候,mysql导入到hdfs导入命令执行以后,在hdfs上面没有找到对应的数据,今天根据这个bug,顺便解决这个问题吧,之前写的http://www.cnblogs.com/ ...

  7. 兆芯 服务器 win2012/win7装机总结

    兆芯cpu 服务器 win2012/win7装机总结 一.设置U盘启动装机 启动后,esc进入bios修改下图两个地方,都要改,然后保存. 二.重启计算机,进入win安装界面,会出现无法安装,原因是: ...

  8. 架构(二)Maven安装以及Nexus配置

    一 Maven安装配置 1.1 下载 http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.5.4/binaries/apache-ma ...

  9. Maven package 报错解决记录以及编译scala的pom.xml

    可以打包的pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:/ ...

  10. 使用bat脚本部署hexo到coding和github

    因项目的不同适当的改造吧,本文以hexo为例. 拉取coding.net的代码和github的代码到本地 确保代码能够正常的运行,commit,push 在项目的目录外新建一个push.bat文件 快 ...