Android中设置控件的背景颜色的方式整理
版权声明:本文为博主原创文章,未经博主允许不得转载。
前言
在Android开发中,经常需要设置控件的背景颜色或者图片的src颜色。
效果图

代码分析
根据使用的方法不同,划分为
- setBackgroundColor方法【一般用于RelativeLayout、TextView等控件】
- 使用colors.xml文件中的颜色
- 使用颜色的int类型值
- 使用颜色的16进制类型值
- setImageDrawable方法【一般用于ImageView控件】
- 使用colors.xml文件中的颜色
- 使用颜色的int类型值
- 使用颜色的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中设置控件的背景颜色的方式整理的更多相关文章
- android中设置控件获得焦点 (转)
android中,要使控件获得焦点,需要先setFocus,再requestFocus. 以Button为例: btn.setFocusable(true); ...
- android中设置控件获得焦点
android中,要使控件获得焦点,需要先setFocus,再requestFocus. 以Button为例: btn.setFocusable(true); ...
- VC、MFC中设置控件的背景色、标题、字体颜色、字体要注意的地方[转]
在MFC中设置控件的背景色.字体.字体颜色.标题等属性主要是利用OnCtlColor函数来实现. 如: HBRUSH CAlarm::OnCtlColor(CDC* pDC, CWnd* pWnd, ...
- MFC中给各个控件填充背景颜色的方法
1.给程序设置大背景色,在OnPaint()函数中添加如下代码: CRect rect; CPaintDC dc(this); GetClientRect(rect); dc.FillSolidRec ...
- Android线程中设置控件
在Android中经常出现多线程中设置控件的值报错的情况,今天教大家封装一个简单的类避免这样的问题,同样也调用实现也非常的方便. 自定义类: /** * Created by wade on 2016 ...
- android中在java代码中设置Button按钮的背景颜色
android中在java代码中设置Button按钮的背景颜色 1.设置背景图片,图片来源于drawable: flightInfoPanel.setBackgroundDrawable(getRes ...
- Android在代码中设置控件的drawableLeft,drawableRight,drawableTop,drawableBottom。
根据业务的需要,要在代码中设置控件的drawableLeft,drawableRight,drawableTop,drawableBottom属性. 我们知道在xml中设置的方法为:android:d ...
- Android 中常见控件的介绍和使用
1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...
- Android中ExpandableListView控件基本使用
本文採用一个Demo来展示Android中ExpandableListView控件的使用,如怎样在组/子ListView中绑定数据源.直接上代码例如以下: 程序结构图: layout文件夹下的 mai ...
随机推荐
- jQuery子页面获取父页面元素并绑定事件
父页面HTML文件: <ul id="faul"> <li class="sonli">子页面列表1</li> <li ...
- C语言在宏定义中使用语句表达式和预处理器运算符
语句表达式的亮点在于定义复杂功能的宏.使用语句表达式来定义宏,不仅可以实现复杂的功能,而且还能避免宏定义带来的歧义和漏洞.下面以一个简单的最小值的宏为例子一步步说明. 1.灰常简单的么,使用条件运算符 ...
- JPA-学习02
一.主键生成策略 主键:确定一张表的唯一性东西(非空且唯一) 分为:自然主键和代理主键. 生成策略: identity:自增策略(1.值必须是数字,2.数据库支持) sequence:序列策略(同上, ...
- gensim自然语言处理
参考代码 ChineseClean_demo1.py: # -*- coding:utf-8 -*- import xlrd import xlwt ''' python3.4 ''' # file ...
- 理解特性attribute 和 属性property的区别 及相关DOM操作总结
查一下英语单词解释,两个都可以表示属性.但attribute倾向于解释为特质,而property倾向于解释私有的.这个property的私有解释可以更方便我们下面的理解. 第一部分:区别点 第一点: ...
- TCP和UDP的优缺点
TCP: 优点: 全双工的可靠连接,使得发送的数据有序.不重复.无差错.不丢失,提供的是可靠的服务: 提供确认重传机制.流量控制和拥塞控制,保证网络的稳定可靠性: 缺点: 每次通信都要建立连接,占用系 ...
- POJ1964-City Game
给你N×M大的矩阵,里面分别有字符‘F'和’R',要找到一个最大的只有‘F'的矩阵,不能包含有’R‘.N,M<=1000. 一开始的思路是单调栈来求最大矩形面积,因为没看清题目不能包含’R'字符 ...
- js打断点
F12打开调试器 资源sources 找到就是文件 选中需要打断点的行 获得段短点的值:将断点向后执行一步(页面提示的桥状小图标),然后选中上一步需要打断点的值,悬浮在上 ...
- Tinkoff Challenge - Final Round (Codeforces Round #414, rated, Div. 1 + Div. 2)
A: 思路:就是找b,c之前有多个s[i] 代码: #include<stdio.h>#define ll long longusing namespace std;ll a,b,c;in ...
- AngularJS 四大特性
1.模块化 2.双向数据绑定 3.依赖注入 4.mvc模式