一、在xml中修改颜色

1、首先贴上/res/values/color色值

<?xml version="1.0" encoding="utf-8"?>    #000000    #ffffff

2、贴上/res/drawable/text_color_selector.xml颜色选择器

<?xml version="1.0" encoding="utf-8"?>        



3、贴上布局/res/layout/activity_main.xml代码

    

二、在代码中修改颜色

我们首先添加一个ColorStateList资源XML文件,XML文件保存在res/color/button_text.xml:

<?xml version="1.0" encoding="utf-8"?>               


Button btn=(Button)findViewById(R.id.btn);  Resources resource=(Resources)getBaseContext().getResources();   ColorStateList csl=(ColorStateList)resource.getColorStateList(R.color.button_text);  if(csl!=null){       btn.setTextColor(color_state_list);//设置按钮文字颜色  }

 

或者可以这样:

 

XmlResourceParser xpp=Resources.getSystem().getXml(R.color.button_text);   try {       ColorStateList csl= ColorStateList.createFromXml(getResources(),xpp);       btn.setTextColor(csl);  } catch (Exception e) {       // TODO: handle exception  } 

运行之后点击按钮就可以看到字体的颜色变化了


更多相关文章

  1. 所谓的沉浸式状态栏
  2. 新浪微博布局学习——妙用TabHost
  3. Android开发底部点击按钮从屏幕底部滑出面板(一)
  4. requires plug-in org.eclipse.wst.sse.ui
  5. Android中常用的颜色
  6. Android(安卓)Cocos2d-x游戏集成友盟社会化组件分享功能
  7. Android(安卓)自定义控件一 带圆形进度的按钮 ControlButton2
  8. Android(安卓)5.x theme: AppCompatActivity与toolbar的结合
  9. android小记之Animation4种动画效果(贴上了GIF图)

随机推荐

  1. Android(安卓)Notification 通知栏点击不
  2. 【Android】Back Home键监听
  3. 【Android】用Eclipse和robotium-solo测
  4. ubuntu kylin 下的 android 编译
  5. Android用Intent和Bundle传list
  6. 抓取Android(安卓)WebView截屏
  7. android 局域网对讲机
  8. Android(安卓)framework 编译常见错误
  9. Android的HTTP协议
  10. Android(安卓)对 SDCARD 卡 上的sqlite3