drawable 定义颜色常数的方法--更改手机窗口画面底色


转自:http://www.eoeandroid.com/thread-537-1-1.html
drawable定义颜色常数的方法--更改手机窗口画面底色

① 编写main布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AbsoluteLayout
  3. android:id="@+id/widget0"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. xmlns:android="http://schemas.android.com/apk/res/android"
  7. >
  8. <TextView
  9. android:id="@+id/name"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="账号"
  13. android:layout_x="61px"
  14. android:layout_y="69px"
  15. >
  16. </TextView>
  17. <TextView
  18. android:id="@+id/password"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:text="密码"
  22. android:layout_x="61px"
  23. android:layout_y="158px"
  24. >
  25. </TextView>
  26. <EditText
  27. android:id="@+id/name_in"
  28. android:layout_width="120dip"
  29. android:layout_height="wrap_content"
  30. android:textSize="18sp"
  31. android:layout_x="114px"
  32. android:layout_y="57px"
  33. >
  34. </EditText>
  35. <EditText
  36. android:id="@+id/pwd_in"
  37. android:layout_width="120dip"
  38. android:layout_height="wrap_content"
  39. android:textSize="18sp"
  40. android:password="true"
  41. android:layout_x="112px"
  42. android:layout_y="142px"
  43. >
  44. </EditText>
  45. </AbsoluteLayout>


② 在values文件夹中定义一个drawable.xml文件 用来存放颜色值

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <color name="white">#FFFFFF</color>
  4. <color name="darkgray">#938192</color>
  5. <color name="lightgreen">#7cd12e</color>
  6. </resources>


③ 修改main.xml中的屏幕背景颜色和TextView的字体颜色
  1. <AbsoluteLayout
  2. android:id="@+id/widget0"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="@color/white"
  6. xmlns:android="http://schemas.android.com/apk/res/android"
  7. >
  8. <TextView
  9. android:id="@+id/name"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="账号"
  13. android:textColor="@color/darkgray"
  14. android:layout_x="61px"
  15. android:layout_y="69px"
  16. >
  17. </TextView>
  18. <TextView
  19. android:id="@+id/password"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="密码"
  23. android:textColor="@color/darkgray"
  24. android:layout_x="61px"
  25. android:layout_y="158px"
  26. >


④ 在mainActivity.java代码中修改TextView背景颜色
  1. public void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. setContentView(R.layout.main);
  4. TextView text=(TextView)findViewById(R.id.name);
  5. //由ID获得资源
  6. Resources myColor=getBaseContext().getResources();
  7. //getBaseContext()获得基础Context
  8. //getResources()获得资源
  9. Drawable color_M=myColor.getDrawable(R.color.lightgreen);
  10. //由资源 myColor来获得DrawableR.color.lightgreen是颜色值的ID引用
  11. text.setBackgroundDrawable(color_M);
  12. //设置背景
  13. }


⑤结果
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值