Set the background color of controls in Android

Source: Internet
Author: User
Tags getcolor

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Objective

In Android development, it is often necessary to set the background color of the control or the SRC color of the image.

Code Analysis

According to the method used, divided into

    • SetBackgroundColor method "generally used for controls such as Relativelayout, TextView, etc."
    1. Using colors from the Colors.xml file
    2. Use the color int type value
    3. Using 16 binary type values for colors
    • Setimagedrawable method "generally used for ImageView controls"
    1. Using colors from the Colors.xml file
    2. Use the color int type value
    3. Using 16 binary type values for colors
//SetBackgroundColor MethodMlayout.setbackgroundcolor (Contextcompat.getcolor ( This, r.color.coloraccent));//using colors from the Colors.xml fileMtvcolorint.setbackgroundcolor (NewInteger (-12590395));//use the color int type valueMtvcolorhex.setbackgroundcolor (Color.parsecolor ("#3FE2C5"));//using 16 binary type values for colors//Setimagedrawable Methoddrawable DrawableColor1 = new colordrawable (Contextcompat.getcolor (this, r.color.coloraccent) ); // use the color in the Colors.xml file "not used here, just to illustrate a way" drawable DrawableColor2 = new colordrawable (new Integer (-2132153879)); // use the color int type value "not used here, just to illustrate a way"  
New Colordrawable (Color.parsecolor ("#80e9e9e9")); // use the color of the 16 binary type value mimgcolor.setimagedrawable (drawablecolor); // Set the Src property value of the ImageView control

Use steps

The Activity_main.xml layout file is as follows:

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns: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= "Use a color value of-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= "Use #3fe2c5 color value"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>

The Mainactivity.java file is as follows:

 PackageCom.why.project.colorutildemo;ImportAndroid.graphics.Color;Importandroid.graphics.drawable.ColorDrawable;Importandroid.graphics.drawable.Drawable;ImportAndroid.os.Bundle;ImportAndroid.support.v4.content.ContextCompat;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.util.Log;ImportAndroid.widget.ImageView;Importandroid.widget.RelativeLayout;ImportAndroid.widget.TextView; Public classMainactivityextendsappcompatactivity {Private Static FinalString TAG = "Mainactivity"; PrivateRelativelayout mlayout; PrivateTextView Mtvcolorint; PrivateTextView Mtvcolorhex; PrivateImageView Mimgcolor; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Initviews ();    InitData (); }    Private voidinitviews () {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 voidInitData () {//SetBackgroundColor MethodMlayout.setbackgroundcolor (Contextcompat.getcolor ( This, r.color.coloraccent));//using colors from the Colors.xml fileMtvcolorint.setbackgroundcolor (NewInteger (-12590395));//use the color int type valueMtvcolorhex.setbackgroundcolor (Color.parsecolor ("#3FE2C5"));//using 16 binary type values for colors//Setimagedrawable Methoddrawable DrawableColor1 =NewColordrawable (Contextcompat.getcolor ( This, r.color.coloraccent));//use the color in the Colors.xml file "not used here, just to illustrate a way"drawable DrawableColor2 =NewColordrawable (NewInteger (-2132153879));//use the color int type value "not used here, just to illustrate a way"drawable Drawablecolor =NewColordrawable (Color.parsecolor ("#80e9e9e9"));//using 16 binary type values for colorsMimgcolor.setimagedrawable (Drawablecolor);//Set the Src property value of the ImageView control    }}

Set the background color of controls in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.