android按钮字体编码,安卓利用按钮Button更改的字体大小、字体颜色、背景颜色代码...

package org.shuxiang.test;

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

import android.view.Window;

import android.widget.Button;

import android.widget.EditText;

import android.widget.LinearLayout;

import android.widget.TextView;

public class MainActivity extends Activity{

private LinearLayout mainLayout;

private Button changeSize, changeColor, changeBgColor;

private EditText fontSize, fontColor, bgColor;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.test4);

mainLayout = (LinearLayout) findViewById(R.id.mainLayout);

changeSize = (Button) findViewById(R.id.ChangeSize);

fontSize = (EditText) findViewById(R.id.fontSize);

changeSize.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v)

{

// TODO Auto-generated method stub

setFontSize(mainLayout, Float.parseFloat(fontSize.getText().toString()));

}

});

changeColor = (Button) findViewById(R.id.ChangeColor);

fontColor = (EditText) findViewById(R.id.fontColor);

changeColor.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v)

{

// TODO Auto-generated method stub

int color = Integer.parseInt(fontColor.getText().toString().replace("#", ""), 16);

int red = (color & 0xff0000) >> 16;

int green = (color & 0x00ff00) >> 8;

int blue = (color & 0x0000ff);

setFontColor(mainLayout, Color.rgb(red, green, blue));

}

});

changeBgColor = (Button) findViewById(R.id.ChangeBgColor);

bgColor = (EditText) findViewById(R.id.bgColor);

changeBgColor.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v)

{

// TODO Auto-generated method stub

int color = Integer.parseInt(bgColor.getText().toString().replace("#", ""), 16);

int red = (color & 0xff0000) >> 16;

int green = (color & 0x00ff00) >> 8;

int blue = (color & 0x0000ff);

setBgColor(mainLayout, Color.rgb(red, green, blue));

}

});

}

/**

* 改变字体

* @param v

* @param fontSize

*/

public void setFontSize(View v, float fontSizeValue)

{

if(v instanceof TextView)

{

((TextView) v).setTextSize(fontSizeValue);

}

else if(v instanceof EditText)

{

((EditText) v).setTextSize(fontSizeValue);

}

else if(v instanceof Button)

{

((Button) v).setTextSize(fontSizeValue);

}

else

{

int vChildCount = ((ViewGroup) v).getChildCount();

for(int i=0; i

{

View v1 = ((ViewGroup) v).getChildAt(i);

setFontSize(v1, fontSizeValue);

}

}

}

/**

* 改变字体颜色

* @param v

* @param fontSize

*/

public void setFontColor(View v, int fontColorValue)

{

if(v instanceof TextView)

{

((TextView) v).setTextColor(fontColorValue);

}

else if(v instanceof EditText)

{

((EditText) v).setTextColor(fontColorValue);

}

else if(v instanceof Button)

{

((Button) v).setTextColor(fontColorValue);

}

else

{

int vChildCount = ((ViewGroup) v).getChildCount();

for(int i=0; i

{

View v1 = ((ViewGroup) v).getChildAt(i);

setFontColor(v1, fontColorValue);

}

}

}

/**

* 改变背景字体

* @param v

* @param fontSize

*/

public void setBgColor(View v, int bgColorValue)

{

if(v instanceof TextView)

{

((TextView) v).setBackgroundColor(bgColorValue);

}

else if(v instanceof EditText)

{

((EditText) v).setBackgroundColor(bgColorValue);

}

else if(v instanceof Button)

{

((Button) v).setBackgroundColor(bgColorValue);

}

else

{

int vChildCount = ((ViewGroup) v).getChildCount();

for(int i=0; i

{

View v1 = ((ViewGroup) v).getChildAt(i);

setBgColor(v1, bgColorValue);

}

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值