自定义流布局FloatLayout(二)之如何在代码中定义shape资源
来源:互联网 发布:智阳网络有年终奖吗 编辑:程序博客网 时间:2024/05/16 07:44
上次讲解了如何定义一个ViewGroup的流布局容器,这次具体讲一下容器中填充的TextView的具体设置
1.动态添加那么设置圆角矩形的时候就不能再Drawable文件下通过创建shape资源来定义了,那么如何在代码中动态的定义圆角矩形的shape呢
2.定义一个获取shapedrawable的类,通过传入参数就可以得到一个shape资源,然后利用随机数对它进行设置颜色 代码如下:
package zz.itcast.googleplay09.utils;import java.util.GregorianCalendar;import android.graphics.Color;import android.graphics.drawable.Drawable;import android.graphics.drawable.GradientDrawable;import android.graphics.drawable.ShapeDrawable;import android.graphics.drawable.StateListDrawable;public class DrawableUtils { /** * 创建圆角矩形 * color:圆角矩形颜色 */ public static Drawable createCornerRect(int color){ //创建圆角矩形的核心类 GradientDrawable gradientDrawable = new GradientDrawable(); //设置圆角矩形的半径 gradientDrawable.setCornerRadius(5); gradientDrawable.setColor(color); return gradientDrawable; } /** * 创建状态选择器 * @param pressedDrawable * @param normalDrawable * @return */ public static Drawable createSelector(Drawable pressedDrawable,Drawable normalDrawable){ StateListDrawable stateListDrawable = new StateListDrawable(); //stateSet 状态集合,可以添加多种状态 stateListDrawable.addState(new int [] {android.R.attr.state_pressed}, pressedDrawable); stateListDrawable.addState(new int [] {}, normalDrawable); return stateListDrawable; } }然后就是在代码中开始使用了
@Override public View createSuccessView() {// TextView textView = new TextView(getActivity());// textView.setText(datas.toString());// return textView; //textivew的padding int textPaddingV = UIUtils.dip2px(4); int textPaddingH = UIUtils.dip2px(7); int space = UIUtils.dip2px(13); ScrollView scrollView = new ScrollView(UIUtils.getAppContext()); FlowLayout flowLayout = new FlowLayout(UIUtils.getAppContext()); flowLayout.setPadding(space, space, space, space);// linearLayout.setOrientation(LinearLayout.VERTICAL);//垂直的线性布局 for (int i = 0; i < datas.size(); i++) { final String content = datas.get(i); TextView textView = new TextView(UIUtils.getAppContext()); textView.setText(content); //0-255 Random random = new Random(); int red = random.nextInt(200)+20;//0-199,20-219这里的处理主要是为了避开纯白色和纯黑色的背景颜色 int green = random.nextInt(200)+20; int blue = random.nextInt(200)+20; //设置背景// textView.setBackground(DrawableUtils.createCornerRect()); int randomColor = Color.rgb(red, green, blue); Drawable normalDrawable = DrawableUtils.createCornerRect(randomColor); Drawable pressedDrawable = DrawableUtils.createCornerRect(R.color.order_text_pressed_color); Drawable selectorDrawable = DrawableUtils.createSelector(pressedDrawable, normalDrawable); textView.setBackgroundDrawable(selectorDrawable); //textView添加padding textView.setPadding(textPaddingH, textPaddingV, textPaddingH, textPaddingV); //textview设置字体大小 textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15); //设置字体颜色 textView.setTextColor(Color.WHITE); //textview添加点击事件,否则TextView默认状态下是不能响应点击事件的,selector也就起不了作用 textView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(UIUtils.getAppContext(), content, Toast.LENGTH_SHORT).show(); } }); //textview宽高都是包裹内容 LayoutParams textViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); flowLayout.addView(textView,textViewLayoutParams); } scrollView.addView(flowLayout); return scrollView; }至此,这个自定义的效果就完全实现了
0 0
- 自定义流布局FloatLayout(二)之如何在代码中定义shape资源
- 自定义流布局FloatLayout(一)
- 分组的自定义流布局方法二
- 如何在QML中定义Javascript资源
- 如何在QML中定义Javascript资源
- drawable自定义图形资源之shape属性
- 如何在代码中动态的设置shape
- 自定义流布局FlowLayout
- Android 自定义流布局
- 自定义流布局FlowGroup
- Android自定义shape资源
- 瀑布流布局代码
- Android资源知识(二)之Shape Drawable的使用
- 图片资源之shape
- 自定义标签布局(流布局)
- 自定义组件---瀑布流布局
- 自定义实现瀑布流布局
- android开发(13) 尝试在流布局中移动控件
- Android RecycleView(三)——增加点击事件
- 从开源项目学习 C 语言基本的编码规则
- 一、Mongodb入门
- 微信群发消息注意事项
- Hadoop-简单的MapReduce案例
- 自定义流布局FloatLayout(二)之如何在代码中定义shape资源
- ./configure参数配置
- android实现只切换本应用的语言(不切换android 系统语言)
- Java之Socket与HTTP区别
- 欢迎使用CSDN-markdown编辑器
- IOS基本类方法与实例方法之日期总结
- qemu网络配置
- 【C#基础】重载和重写(覆盖)
- CSS中属性排序