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);
}
}
}
}
|