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随手记系列】
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决