Android TextView 使用随机背景颜色的方法
来源:互联网 发布:sg是什么软件 编辑:程序博客网 时间:2024/04/28 18:13
在开发中,我们有时候要对控件定义使用随机的颜色,比如我做一个项目,里面有用到标签,但是我不想我的标签只有一种背景颜色框。本文就以下方法给大家介绍如何在安卓中使用随机颜色的控件。
我们通常的做法是在xml文件中对控件写一个background的资源文件。例如:
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tv_topic_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="12dp" android:layout_marginTop="10dp" android:background="@drawable/bg_textview_green_stroke_drawable_no_selec" android:textColor="@color/color_black_232323" android:textSize="12.5dp" />
然后在资源文件中定义相关的颜色属性设置:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/color_white_pure" /> <stroke android:width="1dp" android:color="#61e0fe" /> <padding android:bottom="6dp" android:left="10dp" android:right="10dp" android:top="6dp" /> <corners android:radius="25dp" /></shape>
其实相当于是写死的,所以我们可以从java代码中开搞:
GradientDrawable bgShape = (GradientDrawable)textview.getBackground();// bgShape.setColor(Color.BLACK);bgShape.setStroke(1, Color.parseColor(ColorUtil.getRandomColor()));
getRandomColor是我自己封装的一个随机生成颜色的类,这样我们就可以对自己的控件使用随机生成颜色了。其实还可以设置其他的属性:
// prepare int strokeWidth = 5; // 3px not dp int roundRadius = 15; // 8px not dp int strokeColor = Color.parseColor("#2E3135"); int fillColor = Color.parseColor("#DFDFE0"); bgShape.setColor(fillColor); bgShape.setCornerRadius(roundRadius);
放上一张效果图:
1 0
- Android TextView 使用随机背景颜色的方法
- android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法
- android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法
- 随机背景颜色的生成
- Android TextView的点击效果之改变背景颜色
- Android studio 修改背景颜色的方法!!
- Android设置背景颜色的方法
- android使用selector修改TextView中的字体颜色和背景颜色
- Android 通过按钮Button更改全部的TextView、EditText、Button的字体大小、字体颜色、背景颜色
- Android 通过按钮Button更改全部的TextView、EditText、Button的字体大小、字体颜色、背景颜色
- 富文本的使用之二 设置TextView不同字段的颜色和背景颜色
- 关于android中改变TextView背景颜色以及文字颜色的问题
- Android通过selector选择器设置Button或者textview的背景颜色,字体颜色
- TextView设置超链接、背景颜色、字体颜色等等,Spannable的使用。
- android TextView设置颜色方法
- Android中的如何在代码中使用64位颜色值改变TextView的颜色的方法
- android TextView的字体颜色设置的多种方法
- android TextView的字体颜色设置的多种方法
- c++——复制构造函数的使用
- C#学习篇(四)C#迭代器
- xml 颜色编辑
- MFC中char*,string和CString之间的转换 (转载)
- eclipse支持sun.*包的配置
- Android TextView 使用随机背景颜色的方法
- ionicView 生命周期事件不调用处理方法 $ ionicView.loaded $ ionicView.enter
- AI
- 如何解决CorelDRAW文件损坏问题
- js数组查找
- 【转】PCM Audio,PCM格式音频文件 详解
- Android MaterialList源码解析
- LeetCode21Merge Two Sorted Lists
- 百度都干写什么