Android App设置颜色属性

RGB  :红、绿、蓝,值为0~255。

ARGB:A(透明度,Alpha),0表示完全透明,255表示完全不透明

设置颜色的属性:文字颜色(textColor)  和   背景(background

颜色参数:

1、Color.rgb(255,0,0);  

2、Color.parse("#ffffff");


下面通过一个案例来阐述如何设置颜色属性:

每点击一次按钮,背景颜色和文字颜色更改为任意相同颜色。


multi_color.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/changebg">
   
    <!-- 改变颜色按钮 -->
	<Button android:id="@+id/changeBtn"
	    android:layout_height="40dp"
	    android:layout_width="120dp"
	    android:layout_marginTop="300dp"
	    android:layout_marginLeft="30dp"
	    android:background="@drawable/white"
	    android:text="@string/chgecolor"
	    android:onClick="changeColor"/>
</LinearLayout>


MultiColor.java

package com.application.demo;

import java.util.Random;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MultiColor extends Activity{
	LinearLayout changebg;
	Button changeBtn;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.multi_color);
		setMyLayout();
	}
	public void setMyLayout(){
		changebg = (LinearLayout) findViewById(R.id.changebg);
		changeBtn = (Button) findViewById(R.id.changeBtn);
	}
	public void changeColor(View v){
		int r = new Random().nextInt(256);	//返回0~255之间的任意整数
		int g = new Random().nextInt(256);
		int b = new Random().nextInt(256);
		changebg.setBackgroundColor(Color.rgb(r,g,b));	//更改linearlayout的背景色
		changeBtn.setTextColor(Color.rgb(r,g,b));	//更改btn上的文字颜色
	}
}
运行截图如下:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值