加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

样式表以及Color.xml文件『Android系列六』

发布时间:2020-12-16 09:33:04 所属栏目:百科 来源:网络整理
导读:上篇我们知道了怎么改变TextView标签的各种属性,问题是,如果页面上有几十个上百个同类标签,难道要一个一个的修改吗?马上想到了CSS样式表,这里要庆幸的是Android同样支持样式表的加载。 简单来说只涉及两类文件:layout/main.xml、values/style.xml,下

上篇我们知道了怎么改变TextView标签的各种属性,问题是,如果页面上有几十个上百个同类标签,难道要一个一个的修改吗?马上想到了CSS样式表,这里要庆幸的是Android同样支持样式表的加载。

简单来说只涉及两类文件:layout/main.xml、values/style.xml,下面是他们各自的代码:

main.xml

[html] view plaincopy
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <TextView
  6. style="@style/firstStyle"
  7. android:id="@+id/firstText"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello_world"/>
  11. </LinearLayout>

style.xml
[html] view plaincopy
  1. <resourcesxmlns:android="http://schemas.android.com/apk/res/android">
  2. <stylename="firstStyle">
  3. <itemname="android:textSize">18sp</item>
  4. <itemname="android:textColor">#FF00FF</item>
  5. </style>
  6. </resources>

为了更清晰的证明样式表作用,把main.java代码也贴出来
[java] view plaincopy
  1. packagecom.dy.study.firstbase;
  2. importandroid.os.Bundle;
  3. importandroid.app.Activity;
  4. publicclassMainextendsActivity{
  5. @Override
  6. publicvoidonCreate(BundlesavedInstanceState){
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.main);
  9. }
  10. }

可以注意到,完全没有改变,那么看看效果吧。



再对比一下,把样式表的颜色改为#00FFFF,再看下效果:


样式表简单介绍到这里,接着说color.xml,由于系统提供的Color.GREEN等之类的太少太单薄,所以为了美化UI,还是自己定义多个颜色代码方便使用吧,至于color.xml里面的内容,网上一搜一堆,我在学习的时候就是借鉴其他人共享的资料,这里感谢一下分享者,言归正传,简单列举几个颜色代码,并在main.java里面调用,看看效果:

color.xml

[html] view plaincopy
  1. <resources>
  2. <colorname="pink">#ffc0cb</color><!--粉红色-->
  3. <colorname="white">#ffffff</color><!--白色-->
  4. <colorname="gold">#ffd700</color><!--金色-->
  5. <colorname="indianred">#cd5c5c</color><!--印第安红-->
  6. <colorname="mediumvioletred">#c71585</color><!--中紫罗兰色-->
  7. </resources>

main.java
[java] view plaincopy
  1. packagecom.dy.study.firstbase;
  2. importandroid.os.Bundle;
  3. importandroid.widget.Button;
  4. importandroid.widget.TextView;
  5. importandroid.app.Activity;
  6. publicclassMainextendsActivity{
  7. @Override
  8. publicvoidonCreate(BundlesavedInstanceState){
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.main);
  11. findViews();
  12. change();
  13. }
  14. privateTextViewfirstText;
  15. privateButtonfirstButton;
  16. privatevoidfindViews(){
  17. firstText=(TextView)findViewById(R.id.firstText);
  18. firstButton=(Button)findViewById(R.id.firstButton);
  19. }
  20. privatevoidchange(){
  21. firstText.setTextColor(getResources().getColor(R.color.gold));
  22. firstButton.setTextColor(getResources().getColor(R.color.indianred));
  23. }
  24. }

main.xml

[html] view plaincopy
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <TextView
  6. android:id="@+id/firstText"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="@string/hello_world"/>
  10. <Button
  11. android:id="@+id/firstButton"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="@string/demo"/>
  15. </LinearLayout>


然后看看显示结果:


这里需要注意的是,获取颜色信息需要使用getResources().getColor(R.color.gold)格式。样式表和color文件说完了,愿大家都有一个多彩靓丽的生活。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读