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));//用内部支持的方式设置

  1. package com.oyzz.ch3_6;
  2.  
  3. import android.app.Activity;
  4. /*必须引用graphics.Color才能使用Color.*的对象*/
  5. import android.graphics.Color;
  6. import android.graphics.Typeface;
  7.  
  8. import android.os.Bundle;
  9. import android.view.View;
  10.  
  11. /*必须引用 widget.Button才能声明使用Button对象*/
  12. import android.widget.Button;
  13.  
  14. /*必须引用 widget.TextView才能声明使用TestView对象*/
  15. import android.widget.TextView;
  16. public class Ch3_6 extends Activity
  17. {
  18. private Button mButton;
  19. private TextView mText;
  20. private int[] mColors;
  21. private int colornum;
  22. private Button fontButton;
  23.  
  24. /** Called when the activity is first created. */
  25. @Override
  26.  
  27. public void onCreate(Bundle savedInstanceState)
  28. {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.main);
  31.  
  32. /*通过findViewById构造器来使用main.xml与string.xml
  33. 中button与textView的参数*/
  34. mButton=(Button) findViewById(R.id.mybutton);
  35. mText= (TextView) findViewById(R.id.mytext);
  36. fontButton=(Button) findViewById(R.id.mybutton1);
  37.  
  38. /*声明并构造一整数array来存储欲使用的文字颜色*/
  39. mColors = new int[]
  40. {
  41. Color.BLACK, Color.RED, Color.BLUE,
  42. Color.GREEN, Color.MAGENTA, Color.YELLOW
  43. };
  44. colornum=0;
  45. //得到color.xml文件里的颜色
  46. int b = getResources().getColor(R.drawable.blue);//得到配置文件里的颜色
  47. mButton.setTextColor(b);
  48. /*使用setOnClickListener让按钮聆听事件*/
  49. mButton.setOnClickListener(new View.OnClickListener()
  50. {
  51. /*使用onClick让用户点下按钮来驱动变动文字颜色*/
  52. public void onClick(View v)
  53. {
  54. if (colornum < mColors.length)
  55. {
  56. mText.setTextColor(mColors[colornum]);
  57. colornum++;
  58. }
  59. else
  60. colornum=0;
  61. }
  62. });
  63.  
  64. fontButton.setOnClickListener(new Button.OnClickListener() {
  65. public void onClick(View v) {
  66. mText.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf"));//设置字体
  67. fontButton.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));//用内部支持的方式设置
  68. }
  69. });
  70. }
  71. }
  72.  
  73. main.xml:
  74.  
  75. <?xml version="1.0" encoding="utf-8"?>
  76.  
  77. <!-- Layout使用白色的背景 -->
  78. <LinearLayout
  79. android:id="@+id/widget27"
  80. android:layout_width="fill_parent"
  81. android:layout_height="fill_parent"
  82. android:background="@drawable/white"
  83. xmlns:android="http://schemas.android.com/apk/res/android"
  84. android:orientation="vertical"
  85. >
  86. <!--
  87. 文字使用mytext作為id使用string.xml
  88. textview_str參數 預設文字顏色為按灰色
  89. -->
  90. <TextView
  91. android:id="@+id/mytext"
  92. android:layout_width="wrap_content"
  93. android:layout_height="wrap_content"
  94. android:text="@string/textview_str"
  95. android:textColor="@drawable/darkgray"
  96. >
  97. </TextView>
  98. <!-- 按鈕以mybutton作為id使用string.xml
  99. button_str參數
  100. -->
  101. <Button
  102. android:id="@+id/mybutton"
  103. android:layout_width="wrap_content"
  104. android:layout_height="wrap_content"
  105. android:text="@string/button_str"
  106. >
  107. </Button>
  108. <Button
  109. android:id="@+id/mybutton1"
  110. android:layout_width="wrap_content"
  111. android:layout_height="wrap_content"
  112. android:text="字体"
  113. >
  114. </Button>
  115. </LinearLayout>
  116.  
  117. color.xml:
  118.  
  119. <?xml version="1.0" encoding="utf-8"?>
  120. <resources>
  121. <drawable name="darkgray">#404040ff</drawable>
  122. <drawable name="black">#000</drawable>
  123. <drawable name="red">#ff00ff</drawable>
  124. <drawable name="green">#0ff0ff</drawable>
  125. <drawable name="lightgray">#c0c0c0ff</drawable>
  126. <drawable name="white">#ffffffff</drawable>
  127. <drawable name="yellow">#ffFF33ff</drawable>
  128. <drawable name="blue">#00ffff</drawable>
  129. <drawable name="gray">#808080ff</drawable>
  130. <drawable name="magenta">#ff6699ff</drawable>
  131. <drawable name="cyan">#66ffffff</drawable>
  132. </resources>
  133.  
  134. strings.xml:
  135.  
  136. <?xml version="1.0" encoding="utf-8"?>
  137. <resources>
  138. <string name="hello">Hello World, Ex03_13</string>
  139. <string name="app_name">Ex03_13</string>
  140. <string name="textview_str">转吧七彩霓虹灯</string>
  141. <string name="button_str">按我</string>
  142. </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. C语言遇到的错误和解决方案~~~持续更新,记录成长的过程

    1.error C2296: '&' : illegal, left operand has type 'char [3]' scanf("%d" &x); 少了一 ...

  2. 成为嵌入式程序员应知道的0x10个基本问题

    预处理器(Preprocessor)1 . 用预处理指令#define 声明一个常数,用以表明1年中有多少秒(忽略闰年问题) #define SECONDS_PER_YEAR (60 * 60 * 2 ...

  3. 【CentOs】开机启动与防火墙

    说明: 开机启动使用的命令式chkconfig .防火墙相关的命令式iptables 1.chkconfig 2.iptables 1.chkconfig 参数: --add   新增所指定的系统服务 ...

  4. BZOJ 2820 YY的GCD

    AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=2820 有种方法是枚举质数然后用BZOJ2301来做但是超时了... 具体式子大概张这样: ...

  5. Redis Master/Slave 实践

    本次我们将模拟 Master(1) + Slave(4) 的场景,并通过ASP.NET WEB API进行数据的提交及查询,监控 Redis Master/Slave 数据分发情况,只大致概述,不会按 ...

  6. Sencha Touch 2.4 callParent() 用法

    callParent() 用法 方法介绍 用来调用父类的同名方法,并传参,这在从一个框架类派生且要重写诸如onRender这样的方法时会经常看到. 传参方式 1.arguments Ext.defin ...

  7. VC++之GetLastError()使用说明

    VC中GetLastError()获取错误信息的使用 在VC中编写应用程序时,经常需要涉及到错误处理问题.许多函数调用只用TRUE和FALSE来表明函数的运行结果.一旦出现错误,MSDN中往往会指出请 ...

  8. 讨论下IDS的绕过

    自从知道dedecms自带了80sec的内置Mysqlids后,一直以来也没有想到绕过的办法.或者是自己mysql的根底太差了吧.于是分析dedecms源码时,只找模板执行,本地包含,上传等,完全没有 ...

  9. HDOJ 1085 Holding Bin-Laden Captive! (母函数)

    Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  10. SQL TRY CATCH

    begin try select 1/0end trybegin catch select error_number() as 'number', error_line() as 'line', er ...