上次讲解了如何定义一个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; }至此,这个自定义的效果就完全实现了