TextView动态属性
TextView动态属性
边框
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 设置圆角 -->
<corners android:radius="8px" >
</corners>
<!-- 填充颜色 -->
<solid android:color="#f00" >
</solid>
<!-- 绘制边框 -->
<!--
<stroke
android:width="2px"
android:color="#f00" >
</stroke>
-->
</shape>
setBackGroud-->setDrawble
为Button或者backGroud设置颜色使用GradientDrawable
GradientDrawable gd = new GradientDrawable();
gd.setCornerRadius(UIUtils.dip2Px(5));
int alhpa = 255;//设置透明度
int red = random.nextInt(180) + 30;//设置颜色区间在30-220之间
int green = random.nextInt(180) + 30;
int blue = random.nextInt(180) + 30;
int argb = Color.argb(alhpa, red, green, blue);
gd.setColor(argb);
tv.setBackground(gd);
动态添加点击selector
定义一个selector文件
<item android:drawable="@drawable/btn_pressed"
android:state_pressed="true"/>
<item android:drawable="@drawable/btn_ normal"/>
动态添加StateListDrawable
//添加点击selector
GradientDrawable pressed = new GradientDrawable();
pressed.setCornerRadius(UIUtils.dip2Px(5));
pressed.setColor(Color.DKGRAY);
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[] { android.R.attr.state_pressed }, pressed);
stateListDrawable.addState(new int[] {}, normal);

浙公网安备 33010602011771号