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 ...
随机推荐
- C语言遇到的错误和解决方案~~~持续更新,记录成长的过程
1.error C2296: '&' : illegal, left operand has type 'char [3]' scanf("%d" &x); 少了一 ...
- 成为嵌入式程序员应知道的0x10个基本问题
预处理器(Preprocessor)1 . 用预处理指令#define 声明一个常数,用以表明1年中有多少秒(忽略闰年问题) #define SECONDS_PER_YEAR (60 * 60 * 2 ...
- 【CentOs】开机启动与防火墙
说明: 开机启动使用的命令式chkconfig .防火墙相关的命令式iptables 1.chkconfig 2.iptables 1.chkconfig 参数: --add 新增所指定的系统服务 ...
- BZOJ 2820 YY的GCD
AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=2820 有种方法是枚举质数然后用BZOJ2301来做但是超时了... 具体式子大概张这样: ...
- Redis Master/Slave 实践
本次我们将模拟 Master(1) + Slave(4) 的场景,并通过ASP.NET WEB API进行数据的提交及查询,监控 Redis Master/Slave 数据分发情况,只大致概述,不会按 ...
- Sencha Touch 2.4 callParent() 用法
callParent() 用法 方法介绍 用来调用父类的同名方法,并传参,这在从一个框架类派生且要重写诸如onRender这样的方法时会经常看到. 传参方式 1.arguments Ext.defin ...
- VC++之GetLastError()使用说明
VC中GetLastError()获取错误信息的使用 在VC中编写应用程序时,经常需要涉及到错误处理问题.许多函数调用只用TRUE和FALSE来表明函数的运行结果.一旦出现错误,MSDN中往往会指出请 ...
- 讨论下IDS的绕过
自从知道dedecms自带了80sec的内置Mysqlids后,一直以来也没有想到绕过的办法.或者是自己mysql的根底太差了吧.于是分析dedecms源码时,只找模板执行,本地包含,上传等,完全没有 ...
- HDOJ 1085 Holding Bin-Laden Captive! (母函数)
Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- SQL TRY CATCH
begin try select 1/0end trybegin catch select error_number() as 'number', error_line() as 'line', er ...