Android 通过按钮Button更改全部的TextView EditText Button的字体大小 字体颜色 背景

本文介绍了一种在Android应用中批量修改所有TextView、EditText和Button的字体大小、颜色及背景的方法。通过遍历View并检查类型,实现了动态调整UI元素样式的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

               

本文实现的是自定义设置字体大小、字体颜色、背景颜色,然后通过一键全部修改整个视图内所有的TextView、EditText、Button的字体大小、字体颜色、背景颜色。

实现的逻辑:通过遍历View的方式,判断View是否是TextView、EditText和Button类型,如果是的话,就修改。
http://blog.youkuaiyun.com/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);      }  }     }    }


 

           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值