zoukankan      html  css  js  c++  java
  • Android颜色配置器

    一、Android Color设置

    1、在xml文件中

    想设置颜色直接设置background的属性或者其他的color属性。随便设置一个颜色如#000,再点击左边的颜色方块,弹出颜色选择器选择颜色

    2、在java代码中

    ①Color.parseColor("#000");

    tvShow.setBackgroundColor(Color.parseColor("#000"));

    【提示】可以在布局文件中配置好颜色值,然后把用“#”表示的颜色带到java代码中用

    ②Color.BLACK 使用Color类自带的颜色,不过都是一些基本色

    tvShow.setBackgroundColor(Color.BLACK);

    ③定义Color资源文件,通过R.color.myColor引用

    int color = R.color.myColor;
    tvShow.setBackgroundResource(R.color.myColor);

    ④Color.argb(a,r,g,b)方法:

    tvShow.setBackgroundColor(Color.argb(255, 255, 0, 0));

    分别是alpha、红(red)、绿(green)、蓝(blue)四个颜色值(ARGB)。每个数字取值0-255,因此一个颜色可以用一个整数来表示。为了运行效率,Android编码时用整数Color类实例来表示颜色。

    【提示】通过此方法传入对应的透明度值,红色值,绿色值,蓝色值进行颜色配置。因此我们可以通过此方法做一个简单的颜色配置器。

    二、颜色配置器案例

    1、【效果】

    界面设计的比较粗糙,希望大家能学到实现效果,优化界面。

    2、【项目结构】

    3、【代码】

     ①activity_main.xml布局文件

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     tools:context=".MainActivity"
     7     android:orientation="vertical">
     8 
     9     <TextView
    10         android:text="请输入argb值:"
    11         android:textSize="20sp"
    12         android:textColor="#000"
    13         android:layout_width="match_parent"
    14         android:layout_height="wrap_content"
    15         android:layout_margin="10dp"/>
    16 
    17     <LinearLayout
    18         android:layout_width="match_parent"
    19         android:layout_height="50dp"
    20         android:orientation="horizontal">
    21         <EditText
    22             android:id="@+id/etA"
    23             android:layout_width="0dp"
    24             android:layout_weight="1"
    25             android:layout_height="match_parent"
    26             android:layout_margin="1dp"
    27             android:hint="透明度(0-255)"
    28             android:inputType="number"/>
    29 
    30         <EditText
    31             android:id="@+id/etR"
    32             android:hint="红(0-255)"
    33             android:layout_width="0dp"
    34             android:layout_weight="1"
    35             android:layout_height="match_parent"
    36             android:background="#f00"
    37             android:layout_margin="1dp"
    38             android:gravity="center"
    39             android:inputType="number"/>
    40     </LinearLayout>
    41     <LinearLayout
    42         android:layout_width="match_parent"
    43         android:layout_height="50dp"
    44         android:orientation="horizontal">
    45         <EditText
    46             android:id="@+id/etG"
    47             android:hint="绿(0-255)"
    48             android:layout_width="0dp"
    49             android:layout_weight="1"
    50             android:layout_height="match_parent"
    51             android:background="#0f0"
    52             android:layout_margin="1dp"
    53             android:gravity="center"
    54             android:inputType="number"/>
    55 
    56         <EditText
    57             android:id="@+id/etB"
    58             android:hint="蓝(0-255)"
    59             android:layout_width="0dp"
    60             android:layout_weight="1"
    61             android:layout_height="match_parent"
    62             android:background="#00f"
    63             android:layout_margin="1dp"
    64             android:gravity="center"
    65             android:inputType="number"/>
    66     </LinearLayout>
    67 
    68     <TextView
    69         android:id="@+id/tv_show"
    70         android:text="TextView"
    71         android:layout_width="200dp"
    72         android:layout_height="200dp"
    73         android:background="#000"
    74         android:layout_gravity="center"
    75         android:layout_marginTop="20dp"
    76         />
    77 
    78     <Button
    79         android:id="@+id/btn"
    80         android:text="确定配置"
    81         android:layout_margin="20dp"
    82         android:layout_width="match_parent"
    83         android:layout_height="wrap_content" />
    84 </LinearLayout>

    【提示】EditText 中hint属性:这是设置输入框内的提示文字。 inputType属性:设置输入框输入的文本类型,此处设置为整数型

    ②MainActivity.java文件

     1 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
     2 
     3     private EditText etA;
     4     private EditText etR;
     5     private EditText etG;
     6     private EditText etB;
     7     private TextView tvShow;
     8     private Button btn;
     9 
    10     @Override
    11     protected void onCreate(Bundle savedInstanceState) {
    12         super.onCreate(savedInstanceState);
    13         setContentView(R.layout.activity_main);
    14 
    15         initView();
    16     }
    17 
    18     private void initView() {
    19         etA = (EditText) findViewById(R.id.etA);
    20         etR = (EditText) findViewById(R.id.etR);
    21         etG = (EditText) findViewById(R.id.etG);
    22         etB = (EditText) findViewById(R.id.etB);
    23         tvShow = (TextView) findViewById(R.id.tv_show);
    24         btn = (Button) findViewById(R.id.btn);
    25 
    26         btn.setOnClickListener(this);
    27     }
    28 
    29     @Override
    30     public void onClick(View v) {
    31         switch (v.getId()) {
    32             case R.id.btn:
    33                 submit();
    34                 break;
    35         }
    36     }
    37 
    38     private void submit() {
    39         // validate
    40         if (!etA.getText().equals("")&&!etB.getText().equals("")
    41                 &&!etR.getText().equals("")&&!etG.getText().equals("")) {
    42             //对用户输入的数值进行判断是否为空。避免空字符无法转换为int异常
    43             int et_a = Integer.parseInt(etA.getText().toString());
    44             int et_r = Integer.parseInt(etR.getText().toString());
    45             int et_g = Integer.parseInt(etG.getText().toString());
    46             int et_b = Integer.parseInt(etB.getText().toString());
    47             tvShow.setBackgroundColor(Color.argb(et_a, et_r, et_g, et_b));
    48         }else {
    49             Toast.makeText(this, "输入的值不能为空", Toast.LENGTH_SHORT).show();
    50         }
    51     }
    52 }
  • 相关阅读:
    bzoj1529: [POI2005]ska Piggy banks
    BZOJ 3065 带插入区间K小值
    【BZOJ做题记录】07.07~?
    【BZOJ2882】【字符串的最小表示】工艺
    【不能继续浪啦】BZ做题记录[7.01~7.06]
    第一次到zhzx到今天已经一周年了
    BestCoder Round #45
    BestCoder Round #41 记。
    BZOJ做题记录[0512~?]
    [BZOJ2809&1455&1367]解题报告|可并堆
  • 原文地址:https://www.cnblogs.com/xqz0618/p/8882514.html
Copyright © 2011-2022 走看看