Android颜色配置器配置方法

所属分类: 软件编程 / Android 阅读数: 66
收藏 0 赞 0 分享

一、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布局文件

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity"
 android:orientation="vertical">
 <TextView
  android:text="请输入argb值:"
  android:textSize="20sp"
  android:textColor="#000"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_margin="10dp"/>
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:orientation="horizontal">
  <EditText
   android:id="@+id/etA"
   android:layout_width="0dp"
   android:layout_weight="1"
   android:layout_height="match_parent"
   android:layout_margin="1dp"
   android:hint="透明度(0-255)"
   android:inputType="number"/>
  <EditText
   android:id="@+id/etR"
   android:hint="红(0-255)"
   android:layout_width="0dp"
   android:layout_weight="1"
   android:layout_height="match_parent"
   android:background="#f00"
   android:layout_margin="1dp"
   android:gravity="center"
   android:inputType="number"/>
 </LinearLayout>
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:orientation="horizontal">
  <EditText
   android:id="@+id/etG"
   android:hint="绿(0-255)"
   android:layout_width="0dp"
   android:layout_weight="1"
   android:layout_height="match_parent"
   android:background="#0f0"
   android:layout_margin="1dp"
   android:gravity="center"
   android:inputType="number"/>
  <EditText
   android:id="@+id/etB"
   android:hint="蓝(0-255)"
   android:layout_width="0dp"
   android:layout_weight="1"
   android:layout_height="match_parent"
   android:background="#00f"
   android:layout_margin="1dp"
   android:gravity="center"
   android:inputType="number"/>
 </LinearLayout>
 <TextView
  android:id="@+id/tv_show"
  android:text="TextView"
  android:layout_width="200dp"
  android:layout_height="200dp"
  android:background="#000"
  android:layout_gravity="center"
  android:layout_marginTop="20dp"
  />
 <Button
  android:id="@+id/btn"
  android:text="确定配置"
  android:layout_margin="20dp"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" />
</LinearLayout>

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

②MainActivity.java文件

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 private EditText etA;
 private EditText etR;
 private EditText etG;
 private EditText etB;
 private TextView tvShow;
 private Button btn;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initView();
 }
 private void initView() {
  etA = (EditText) findViewById(R.id.etA);
  etR = (EditText) findViewById(R.id.etR);
  etG = (EditText) findViewById(R.id.etG);
  etB = (EditText) findViewById(R.id.etB);
  tvShow = (TextView) findViewById(R.id.tv_show);
  btn = (Button) findViewById(R.id.btn);
  btn.setOnClickListener(this);
 }
 @Override
 public void onClick(View v) {
  switch (v.getId()) {
   case R.id.btn:
    submit();
    break;
  }
 }
 private void submit() {
  // validate
  if (!etA.getText().equals("")&&!etB.getText().equals("")
    &&!etR.getText().equals("")&&!etG.getText().equals("")) {
   //对用户输入的数值进行判断是否为空。避免空字符无法转换为int异常
   int et_a = Integer.parseInt(etA.getText().toString());
   int et_r = Integer.parseInt(etR.getText().toString());
   int et_g = Integer.parseInt(etG.getText().toString());
   int et_b = Integer.parseInt(etB.getText().toString());
   tvShow.setBackgroundColor(Color.argb(et_a, et_r, et_g, et_b));
  }else {
   Toast.makeText(this, "输入的值不能为空", Toast.LENGTH_SHORT).show();
  }
 }
}

以上所述是小编给大家介绍的Android颜色配置器配置方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多