• 更改TextView背景色——编码定义颜色


    res/values/color.xml
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="red">#ffff0000</color>
    </resources>
    src/EX03_03.java
    package gphone.ex03_03;
    
    import android.app.Activity;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.widget.TextView;
    public class EX03_03 extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Resources resources=this.getBaseContext().getResources();
            int color=resources.getColor(R.color.red);
            TextView tv=(TextView)this.findViewById(R.id.text);
            tv.setBackgroundColor(color);
        }
    }

    或颜色在编码时定义使用系统定义颜色:

    package gphone.ex03_03;
    
    import android.app.Activity;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.widget.TextView;
    public class EX03_03 extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            /*Resources resources=this.getBaseContext().getResources();
            int color=resources.getColor(R.color.red);*/
            TextView tv=(TextView)this.findViewById(R.id.text);
            
            tv.setBackgroundColor(android.graphics.Color.RED);
        }
    }

    image

  • 相关阅读:
    [转载]Sublime Text 3 搭建 React.js 开发环境
    浏览器缓存之Expires Etag Last-Modified max-age详解
    第16周作业
    第15周作业
    第14周作业
    第13周作业集
    软件工程结课作业
    第13次作业--邮箱的正则表达式
    第12次作业--你的生日
    第11次作业--字符串处理
  • 原文地址:https://www.cnblogs.com/AlexCheng/p/2120054.html
Copyright © 2020-2023  润新知