Android中设置控件的背景颜色的方式整理

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

前言

在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属性值

    }
}
原文地址:https://www.cnblogs.com/whycxb/p/6851660.html