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:
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"
>
android:id="@+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textview_str"
android:textcolor="@drawable/darkgray"
>
android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_str"
>
android:id="@+id/mybutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="字体"
>
color.xml:
#404040ff
#000
#ff00ff
#0ff0ff
#c0c0c0ff
#ffffffff
#ffff33ff
#00ffff
#808080ff
#ff6699ff
#66ffffff
strings.xml:
hello world, ex03_13
ex03_13
转吧七彩霓虹灯
按我