自定义TextView 灵活使用Shape实现边框
来源:互联网 发布:保益读屏软件苹果下载 编辑:程序博客网 时间:2024/05/21 14:59
效果:
一般写法,使用shape:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <stroke android:width="1dp" android:color="@color/google_lightYellow"></stroke> <corners android:radius="5dp"></corners></shape>
灵活写法:
- 在attrs.xml中自定义属性
<declare-styleable name="CustomShapeTextView"> <attr name="default_ring_width" format="dimension" /> <attr name="default_ring_color" format="color" /> <attr name="default_ring_angle" format="dimension" /> </declare-styleable>
自定义TextView
private static final int DEFAULT_COLOR = R.color.google_lightGreen;
private static final int DEFAULT_RING_W = 2;
private static final int DEFAULT_RING_ANGLE = 5;
//边线颜色
private int mRingColor = DEFAULT_COLOR;
//边线宽度
private int mRingWidth = DEFAULT_RING_W;
//边线圆角
private int mRingAngle = DEFAULT_RING_ANGLE;private Paint mPaint;
public CustomShapeTextView(Context context) {
super(context);
init();
}public CustomShapeTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}public CustomShapeTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomShapeTextView, defStyleAttr, 0);
mRingColor = a.getColor(R.styleable.CustomShapeTextView_default_ring_color, getResources().getColor(DEFAULT_COLOR));
mRingWidth = (int) a.getDimension(R.styleable.CustomShapeTextView_default_ring_width, DEFAULT_RING_W);
mRingAngle = (int) a.getDimension(R.styleable.CustomShapeTextView_default_ring_angle, DEFAULT_RING_ANGLE);
a.recycle();
init();
}private void init() {
GradientDrawable drawable = new GradientDrawable();
drawable.setCornerRadius(mRingAngle);
drawable.setStroke(mRingWidth, mRingColor);
setBackground(drawable);
}
“`使用
可以再扩展。
- 自定义TextView 灵活使用Shape实现边框
- Android代码实现Shape,自定义TextView
- 使用shape自定义TextView的三态
- 自定义边框shape的介绍
- 自定义带shape的TextView
- 使用shape或者layer-list实现view周边添加边框
- Android开发之Shape的使用(为TextView等空间添加边框)
- Android通过shape实现边框
- Android通过shape实现边框
- 圆角边框shape实现
- Android 使用shape添加边框
- android shape的使用 边框
- Android之TextView灵活使用
- Android UI设计之<四>自定义TextView属性,实现带边框效果的TextView
- 自定义Textview背景 -------》shape 各属性含义
- 使用 自定义布局 实现灵活的万能遥控器界面
- android shape实现边框圆角
- shape边框
- mysql的timestamp类型
- JFreeChart生成的图片在IE和360浏览器上不显示
- JdbcTemplate
- python代码`if not x:` 和`if x is not None:`和`if not x is None:`使用
- Java对【JSON数据的解析】--Gson解析法
- 自定义TextView 灵活使用Shape实现边框
- 工业控制信息安全资源汇总(国内篇)
- 关于css选择器的小结
- C++第4次实验(基础班)—循环结构程序设计(参考答案)-项目3:乘法口诀表
- eclipse中git分支创建与合并
- 案例十四、css3和fullpage实现全屏滚动
- Binding基础
- Mac下Android开发环境搭建
- Video Analysis 相关领域介绍之Video Captioning(视频to文字描述)