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>

代码

android 设置控件的颜色,字体的更多相关文章

  1. Android 设置控件可见与不可见

    通常控件的可见与不可见分为三种情况 第一种    gone         表示不可见并且不占用空间 第二种    visible       表示可见 第三种    invisible    表示不 ...

  2. android 给控件使用自定义字体Typeface

    第一步:将字体资源放在assets 资源文件夹下: 第二步:获取字体资源 Typeface mTf = Typeface.createFromAsset(c.getAssets(), "Op ...

  3. android在代码中四种设置控件背景颜色的方法(包含RGB)

    转载请注明出处: http://blog.csdn.net/fth826595345/article/details/9208771  TextView tText=(TextView) findVi ...

  4. VC OnCtlColor函数来修改控件背景颜色

    CWnd::OnCtlColor afx_msg HBRUSH OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor ); 返回值:OnCtlColor必须 ...

  5. VC、MFC中设置控件的背景色、标题、字体颜色、字体要注意的地方[转]

    在MFC中设置控件的背景色.字体.字体颜色.标题等属性主要是利用OnCtlColor函数来实现. 如: HBRUSH CAlarm::OnCtlColor(CDC* pDC, CWnd* pWnd, ...

  6. Android中设置控件的背景颜色的方式整理

    版权声明:本文为博主原创文章,未经博主允许不得转载. 前言 在Android开发中,经常需要设置控件的背景颜色或者图片的src颜色. 效果图 代码分析 根据使用的方法不同,划分为 setBackgro ...

  7. Android线程中设置控件

    在Android中经常出现多线程中设置控件的值报错的情况,今天教大家封装一个简单的类避免这样的问题,同样也调用实现也非常的方便. 自定义类: /** * Created by wade on 2016 ...

  8. android 开发-设置控件/view的水平方向翻转

    设置控件沿着水平方向翻转(即Y轴180°) 看效果: 代码: <pl.droidsonroids.gif.GifImageView android:id="@+id/gv_image1 ...

  9. MFC控件的颜色设置

    在绘制控件颜色时,控件会发送WM_CTLCOLOR消息给父窗口,父窗口收到消息后,映射到OnCtlColor()函数中处理. 该函数返回一个画刷用于设置子控件的背景颜色,子控件再执行自己的CtlCol ...

随机推荐

  1. css3动画实例

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. 关于myeclipse代码提示的一些问题

    默认是  .xxx  输入点提示,要写注释 @xxx的时候怎么输入@后面有代码提示呢? Auto activation delay 是代码提示出现的速度  下面一行是出现代码提示的条件 我们在.后面加 ...

  3. asp.net 分布式缓存

    之前Velocity已被 集成到App Fabric(包含有WCF监控==)中.   网络Velocity使用大多是针对老版本:  老版本的下载地址:  http://www.microsoft.co ...

  4. 常量折叠 const folding

    http://bbs.byr.cn/#!article/CPP/86336?p=1 下列代码给出输出结果: #include"stdafx.h" #include <iost ...

  5. android SDK更新

    在proxy.ini里的[profile]下加上如下配置即可更新android SDK了 dl-ssl.google.com = nofakehttps Oct 26, 2014 #2 2828qw. ...

  6. 关于面向对象--oop

    这两天在做大数据方面的项目看到关于job作业调度的设计,扣了两天了,感触良多,记下来做个反省. 这是一个精简版的图,其中还有一些没有划到,其实到这里目前对我来说已经足够了. 看完图之后进行分析,我只抛 ...

  7. hdu 2094 产生冠军(STL,set)

    题目 //把所有的出现的名字开始默认都为冠军(1),然后输了的置为0,表示不为冠军,最后统计不为0的, //当有且只有一个不为0的,这个就为冠军,否则,不能产生冠军. //以上思路来自别人的博客.. ...

  8. POJ 1845 Sumdiv (求某个数的所有正因子的和)

    题意: 求A^B的所有正因子的和,最后模9901的结果. 思路: 若对一个数n进行素数分解,n=p1^a1*p2^a2*p3^a3*...*pk^ak那么n的所有正因子之和sum=(1+p1+...+ ...

  9. spring 注入失败

    最近发现autowired注入总是失败. 总结下: 一个bean 要么都通过getter setter加上配置文件配置注入. <bean id="temResetService&quo ...

  10. 欧拉工程第54题:Poker hands

    package projecteuler51to60; import java.awt.peer.SystemTrayPeer; import java.io.BufferedReader; impo ...