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

前端之家收集整理的这篇文章主要介绍了样式表以及Color.xml文件『Android系列六』前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

main.xml

  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>
  12. @H_404_200@

style.xml
  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>
  7. @H_404_200@

为了更清晰的证明样式表作用,把main.java代码也贴出来
@H_403_373@
  • packagecom.dy.study.firstbase;
  • importandroid.os.Bundle;
  • importandroid.app.Activity;
  • publicclassMainextendsActivity{
  • @Override
  • publicvoidonCreate(BundlesavedInstanceState){
  • super.onCreate(savedInstanceState);
  • setContentView(R.layout.main);
  • }
  • }
  • @H_404_200@

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



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


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

    color.xml

    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>
    8. @H_404_200@

    main.java
    @H_403_373@
  • packagecom.dy.study.firstbase;
  • importandroid.os.Bundle;
  • importandroid.widget.Button;
  • importandroid.widget.TextView;
  • importandroid.app.Activity;
  • publicclassMainextendsActivity{
  • @Override
  • publicvoidonCreate(BundlesavedInstanceState){
  • super.onCreate(savedInstanceState);
  • setContentView(R.layout.main);
  • findViews();
  • change();
  • }
  • privateTextViewfirstText;
  • privateButtonfirstButton;
  • privatevoidfindViews(){
  • firstText=(TextView)findViewById(R.id.firstText);
  • firstButton=(Button)findViewById(R.id.firstButton);
  • }
  • privatevoidchange(){
  • firstText.setTextColor(getResources().getColor(R.color.gold));
  • firstButton.setTextColor(getResources().getColor(R.color.indianred));
  • }
  • }
  • @H_404_200@

    main.xml

    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>
    16. @H_404_200@


    然后看看显示结果:


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

    原文链接:https://www.f2er.com/xml/297982.html

    猜你在找的XML相关文章