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屬性值

    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM