shape的对应的代码写法
来源:互联网 发布:工信部 域名备案 编辑:程序博客网 时间:2024/06/06 05:57
在界面设置时,我们很多时候需要给控件加上圆角或边颜色的效果,一般来说我们可以设置其background为一个shape类型的drawable即可实现,类似代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/ll_root" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:background="@drawable/rec_bank_white_purple5" android:orientation="horizontal"/>此背景即为drawable下的一个资源文件,类似代码如下:<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/white" /> <corners android:radius="5dp" /> <stroke android:width="1dp" android:color="@color/questionBankLightPurple" /></shape>我们通过代码ll_root.getBackground();
获取得drawable,发现其实返回的是一个 android.graphics.drawable.GradientDrawable;
因此我们就可以用代码来生成这个backgroud了/** * 产生shape类型的drawable * * @param solidColor * @param strokeColor * @param strokeWidth * @param radius * @return */public static GradientDrawable getBackgroundDrawable(int solidColor, int strokeColor, int strokeWidth, float radius) { GradientDrawable drawable = new GradientDrawable(); drawable.setColor(solidColor); drawable.setStroke(strokeWidth, strokeColor); drawable.setCornerRadius(radius); return drawable;}
此方法对应上面的shape就一目了然了,这样在代码中使用可以实现ListView下有不同边框颜色的条目了,而不必要去生成过多的drawable资料文件。
0 0
- shape的对应的代码写法
- sql对应mongodb的写法
- trim对应正则下几种写法的区别
- uml对应的hibernate映射解析写法
- font-family的中文字体对应英文写法
- 伪代码的写法
- 伪代码的写法
- 伪代码的写法
- 伪代码的写法
- 伪代码的写法
- 通过代码实现android shape的功能
- 代码修改shape的颜色属性
- Android 代码修改shape的颜色属性
- 一个代码效率的写法
- JavaScript代码最好的写法
- python代码的通用写法
- 算法伪代码的写法
- 数学公式对应的markdown代码
- 抽象类与接口
- EOF和'\0'
- android activity的管理类的实现
- IOS学习笔记——基础界面 (二)
- 感知哈希算法(Perceptual hash algorithm)
- shape的对应的代码写法
- 关于在64位win7下运行Virtualbox安装系统时出错(提示VBoxDD.DLL错误)的解决方案
- mysql执行SQL脚本,SQL语句入门
- linux下使用tar命令
- Leetcode 13. Roman to Integer
- java设计模式之桥接模式
- 第十五周实践项目阅读程序————4
- 为什么有些事我们明知道是正确的的,却不付诸行动呢?
- 【MyBatis学习01】宏观上把握MyBatis框架