android 设置控件的颜色,字体
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 设置控件的颜色,字体的更多相关文章
- Android 设置控件可见与不可见
通常控件的可见与不可见分为三种情况 第一种 gone 表示不可见并且不占用空间 第二种 visible 表示可见 第三种 invisible 表示不 ...
- android 给控件使用自定义字体Typeface
第一步:将字体资源放在assets 资源文件夹下: 第二步:获取字体资源 Typeface mTf = Typeface.createFromAsset(c.getAssets(), "Op ...
- android在代码中四种设置控件背景颜色的方法(包含RGB)
转载请注明出处: http://blog.csdn.net/fth826595345/article/details/9208771 TextView tText=(TextView) findVi ...
- VC OnCtlColor函数来修改控件背景颜色
CWnd::OnCtlColor afx_msg HBRUSH OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor ); 返回值:OnCtlColor必须 ...
- VC、MFC中设置控件的背景色、标题、字体颜色、字体要注意的地方[转]
在MFC中设置控件的背景色.字体.字体颜色.标题等属性主要是利用OnCtlColor函数来实现. 如: HBRUSH CAlarm::OnCtlColor(CDC* pDC, CWnd* pWnd, ...
- Android中设置控件的背景颜色的方式整理
版权声明:本文为博主原创文章,未经博主允许不得转载. 前言 在Android开发中,经常需要设置控件的背景颜色或者图片的src颜色. 效果图 代码分析 根据使用的方法不同,划分为 setBackgro ...
- Android线程中设置控件
在Android中经常出现多线程中设置控件的值报错的情况,今天教大家封装一个简单的类避免这样的问题,同样也调用实现也非常的方便. 自定义类: /** * Created by wade on 2016 ...
- android 开发-设置控件/view的水平方向翻转
设置控件沿着水平方向翻转(即Y轴180°) 看效果: 代码: <pl.droidsonroids.gif.GifImageView android:id="@+id/gv_image1 ...
- MFC控件的颜色设置
在绘制控件颜色时,控件会发送WM_CTLCOLOR消息给父窗口,父窗口收到消息后,映射到OnCtlColor()函数中处理. 该函数返回一个画刷用于设置子控件的背景颜色,子控件再执行自己的CtlCol ...
随机推荐
- 老陈 ASP.NET封装
第一个页面 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...
- ZOJ 3941 Kpop Music Party 贪心
题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3941 题解: 先吧所给的区间合并,得到若干个独立的区间. 然后从左 ...
- BitMap排序
问题描述: BitMap排序思想: 用1bit位标记某个元素对应的值 优点: 效率高,不允许进行比较和移位 ...
- DOM操作样式表及其兼容性
DOM操作样式表的时候,存在很多浏览器兼容上的问题,测试的时候用的是Firefox 28.0.IE11.IE8.Chrome.测试的时候发现,不兼容问题基本上都是IE8和非IE浏览器之家的问题,很多I ...
- BZOJ 1012: [JSOI2008]最大数maxnumber 线段树
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能: ...
- 【BZOJ】【1485】【HNOI2009】有趣的数列
Catalan数/组合数取模 Aha!这题我突然灵光一现就想到Catalan数……就是按顺序安排1~2n这些数(以满足前两个条件)……分配到奇数位置上的必须比偶数位置上的多(要不就不满足第三个条件了) ...
- Leetcode#128 Longest Consecutive Sequence
原题地址 1. 把所有元素都塞到集合里2. 遍历所有元素,对于每个元素,如果集合里没有,就算了,如果有的话,就向左向右拓展,找到最长的连续范围,同时在每次找的时候都把找到的删掉.这样做保证了同样的连续 ...
- Poj 1255 覆盖的面积 2014-07-28 12:29 116人阅读 评论(0) 收藏
覆盖的面积 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 解决IE不支持position:fixed问题
#box { /* 非IE6浏览器使用固定元素 */ position:fixed; top:0; left:0; /* IE6改为绝对定位,并通过css表达式根据滚动位置更改top的值 */ _po ...
- Linux Rsync
一.Rsync介绍 1.什么是Rsync Rsync 即Remote Rynchronization,是一款开源的.快速的.多功能的.可实现全量或增量的本地或者远程数据镜像同步复制.备份的优秀工具. ...