当前位置: 移动技术网 > 移动技术>移动开发>Android > android 设置控件的颜色字体的方法

android 设置控件的颜色字体的方法

2019年07月24日  | 移动技术网移动技术  | 我要评论

1.用代码设置控件的颜色:

复制代码 代码如下:

    int b =  getresources().getcolor(r.drawable.blue);//得到配置文件里的颜色
    mbutton.settextcolor(b);
   
2.设置空间的字体:
方式一:mtext.settypeface(typeface.createfromasset(getassets(),"fonts/handmadetypewriter.ttf"));//设置字体
   注意:1.保证文件一定是ttf格式;2.放到assets/fonts目录下;3.如果找不到相应的字体不会报错,只是在运行的时候显示不出来
方式二: fontbutton.settypeface(typeface.defaultfromstyle(typeface.italic));//用内部支持的方式设置

复制代码 代码如下:

package com.oyzz.ch3_6;

import android.app.activity;
/*必须引用graphics.color才能使用color.*的对象*/
import android.graphics.color;
import android.graphics.typeface;

import android.os.bundle;
import android.view.view;

/*必须引用 widget.button才能声明使用button对象*/
import android.widget.button;

/*必须引用 widget.textview才能声明使用testview对象*/
import android.widget.textview;
public class ch3_6 extends activity
{
  private button mbutton;
  private textview mtext;
  private int[] mcolors;
  private int colornum;
  private button fontbutton;

  /** called when the activity is first created. */
  @override

  public void oncreate(bundle savedinstancestate)
  {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);

    /*通过findviewbyid构造器来使用main.xml与string.xml
    中button与textview的参数*/
    mbutton=(button) findviewbyid(r.id.mybutton);
    mtext= (textview) findviewbyid(r.id.mytext);
    fontbutton=(button) findviewbyid(r.id.mybutton1);

    /*声明并构造一整数array来存储欲使用的文字颜色*/
    mcolors = new int[]
                      {
    color.black, color.red, color.blue,
    color.green, color.magenta, color.yellow
                       };
    colornum=0;
    //得到color.xml文件里的颜色
    int b =  getresources().getcolor(r.drawable.blue);//得到配置文件里的颜色
    mbutton.settextcolor(b);
    /*使用setonclicklistener让按钮聆听事件*/
    mbutton.setonclicklistener(new view.onclicklistener()
    {            
      /*使用onclick让用户点下按钮来驱动变动文字颜色*/
      public void onclick(view v)
      {       
        if (colornum < mcolors.length)
        {
          mtext.settextcolor(mcolors[colornum]);
          colornum++;
        }
        else
          colornum=0;
       } 
    });

    fontbutton.setonclicklistener(new button.onclicklistener() {
  public void onclick(view v) {
   mtext.settypeface(typeface.createfromasset(getassets(),"fonts/handmadetypewriter.ttf"));//设置字体
   fontbutton.settypeface(typeface.defaultfromstyle(typeface.italic));//用内部支持的方式设置
  }
 });
  }
}


main.xml:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>

    <!-- layout使用白色的背景 -->
<linearlayout
  android:id="@+id/widget27"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/white"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  >
  <!--
      文字使用mytext作為id使用string.xml中
      的textview_str參數 預設文字顏色為按灰色
  -->
  <textview
  android:id="@+id/mytext"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/textview_str"
  android:textcolor="@drawable/darkgray"
  >
  </textview>
  <!-- 按鈕以mybutton作為id使用string.xml中
   的button_str參數
  -->
  <button
  android:id="@+id/mybutton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/button_str"
  >
  </button>
   <button
  android:id="@+id/mybutton1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="字体"
  >
  </button>
</linearlayout>

color.xml:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
  <resources>
  <drawable name="darkgray">#404040ff</drawable>
  <drawable name="black">#000</drawable>
  <drawable name="red">#ff00ff</drawable>
  <drawable name="green">#0ff0ff</drawable>
  <drawable name="lightgray">#c0c0c0ff</drawable>
  <drawable name="white">#ffffffff</drawable>
  <drawable name="yellow">#ffff33ff</drawable>
  <drawable name="blue">#00ffff</drawable>
  <drawable name="gray">#808080ff</drawable>
  <drawable name="magenta">#ff6699ff</drawable>
  <drawable name="cyan">#66ffffff</drawable>
</resources>

strings.xml:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="hello">hello world, ex03_13</string>
  <string name="app_name">ex03_13</string>
  <string name="textview_str">转吧七彩霓虹灯</string>
  <string name="button_str">按我</string>
</resources>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网