本文实现的是自定义设置字体大小、字体颜色、背景颜色,然后通过一键全部修改整个视图内所有的TextView、EditText、Button的字体大小、字体颜色、背景颜色。
实现的逻辑:通过遍历View的方式,判断View是否是TextView、EditText和Button类型,如果是的话,就修改。
http://blog.csdn.net/aminfo/article/details/7796401
代码如下:
1、xml布局文件,文件名:test4.xml,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/mainLayout">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText android:id="@+id/fontSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30"
android:hint="请输入数字"/>
<Button android:id="@+id/ChangeSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="改变字体"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText android:id="@+id/fontColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#ffffff"
android:hint="请输入字体颜色"/>
<Button android:id="@+id/ChangeColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="改变字体颜色" />
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText android:id="@+id/bgColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#ff0000"
android:hint="请输入背景颜色"/>
<Button android:id="@+id/ChangeBgColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="改变背景颜色"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="top"
android:gravity="top"
android:textColor="#ffffff"
android:background="#00ff00"
android:layout_margin="2px"/>
<TextView android:id="@+id/TextView02"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="bottom"
android:gravity="bottom"
android:textColor="#ffffff"
android:background="#00ff00"
android:layout_margin="2px"/>
<TextView android:id="@+id/TextView03"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="left"
android:gravity="left"
android:textColor="#ffffff"
android:background="#00ff00"
android:layout_margin="2px"/>
<TextView android:id="@+id/TextView04"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="right"
android:gravity="right"
android:textColor="#ffffff"
android:background="#00ff00"
android:layout_margin="2px"/>
<TextView android:id="@+id/TextView05"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="center_vertical"
android:gravity="center_vertical"
android:textColor="#ffffff"
android:background="#00ff00"
android:layout_margin="2px"/>
<TextView android:id="@+id/TextView06"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="fill_vertical"
android:gravity="fill_vertical"
android:textColor="#ffffff"
android:background="#00ff00"
android:layout_margin="2px"/>
<TextView android:id="@+id/TextView07"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="center_horizontal"
android:gravity="center_horizontal"
android:textColor="#ffffff"
android:background="#00ff00"
android:layout_margin="2px"/>
<TextView android:id="@+id/TextView08"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="fill_horizontal"
android:gravity="fill_horizontal"
android:textColor="#ffffff"
android:background="#00ff00"
android:layout_margin="2px"/>
<TextView android:id="@+id/TextView09"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="center"
android:gravity="center"
android:textColor="#ffffff"
android:background="#00ff00"
android:layout_margin="2px"/>
<TextView android:id="@+id/TextView10"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="fill"
android:gravity="fill"
android:textColor="#ffffff"
android:background="#00ff00"
android:layout_margin="2px"/>
<TextView android:id="@+id/TextView11"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="clip_vertical"
android:gravity="clip_vertical"
android:textColor="#ffffff"
android:background="#00ff00"
android:layout_margin="2px"/>
<TextView android:id="@+id/TextView12"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="clip_horizontal"
android:gravity="clip_horizontal"
android:textColor="#ffffff"
android:background="#00ff00"
android:layout_margin="2px"/>
</LinearLayout>
</LinearLayout>
2、实现的代码文件:MainActivity.java,代码如下:
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<vChildCount; 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<vChildCount; 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<vChildCount; i++)
{
View v1 = ((ViewGroup) v).getChildAt(i);
setBgColor(v1, bgColorValue);
}
}
}
}
分享到:
相关推荐
基本控件案例集锦,Button,TextView,EditText,Toast,RadioButton 基本控件案例集锦,Button,TextView,EditText,Toast,RadioButton 基本控件案例集锦,Button,TextView,EditText,Toast,RadioButton 基本...
常用控件:TextView EditText Button 项目文件
本篇文章将使用EditText(编辑文本框)、TextView(文本框),以及Button(命令按钮)组件来模仿设计一个友好的登录界面,当“学号”、“姓名”、“密码”文本框中都输入后,点击登录,如果密码正确则显示“快跑呀,XX...
根据mars老师的视频,自己也照模样写了个简单的例子,适合初学者看,学习!代码有详细注释
4、如果一切正常,那么你会在虚拟设备看到常用的控件(包括TextView, EditText,AutoCompleteTextView,MultiAutoCompleteTextView,Button,ImageButton,ToggleButton,CheckBox和RadioButton),根据中文提示操作。...
edittext 与textview 所有属性详解
自定义textview 实现字体竖排效果
Android更改与现实文字标签(TextView标签的使用)
android新手数据存储之文件存储 新建一个工程,依次添加EditText,Button,TextView,Button 2)点击第一个Button时,把EditText中输入的内容写入到文件中 3)点击第二个Button时,读出文件中的内容显示到TextView中
TextView是Android中很常用的用于显示文字(字符串)的控件,可以在代码中设置TextView控件属性,改变文字的大小、颜色、样式等。 其中: android:layout_width和android:layout_height用于设置控件的宽和高,match_...
自己总结的几点。。,关于在TextView中进行图文展现,,这个方法同样适用在Button,EditText中使用..
一个可以用代号处理控件的阴影效果,以及用代号在TextView、EditText、Button等控件设置selector.zip
顶端一行由:textview,edittext和button从左到右构成,一开始edittext不获取焦点,只显示textview和edittext,当edittext获取焦点的时候,textview和edittext一起向左侧滑至textview消失,button显示...当edittext失去焦点...
android常用基本功能实现...0 android 创建按钮 Button button = new Button(this); 1 android 创建输入框 EditText editText = new EditText(this); 2 android 创建文本 TextView textView = new TextView(this);
在布局文件中包括TextView , Button , EditText或RadioButton 。 < com .koonat.easyfont.TextView android : layout_width = " wrap_content " android : layout_height = " wrap_content " android : t
#Android简单实现加减乘除(两个EditText,两个TextView,四个button) ##界面效果 MainActivity.java package com.example.test1_1; import android.app.Activity; import android.os.Bundle; import ...
Android Studio目前所学习的9个控件代码 后续随着学习进度还会不断更新 TextView Button EditText ImageView ProgressBar Notification Toolbar AlertDialog PopupWindow
如何在Kotlin Android中使用EditText和Button进行输入...
ShapeView打造万能shape,再也不用写很多xml了,可以当做TextView,Button,EditText等多种控件,方便实用效果图属性说明(以下属性全部可以通过xml文件配置和代码进行设置)属性名描述默认值类型sGravity文字对齐方式 ...