zoukankan      html  css  js  c++  java
  • 自定义圆角背景的textview,抛弃shape

    自定义一个圆角背景的TextView,解放双手,不用再写shape了。

    1.values目录新建attrs.xml。

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="test">
            <attr name="radius" format="integer" />
            <attr name="stokeWidth" format="float" />
            <attr name="stokeColor" format="reference|color"/>
            <attr name="solidColor" format="reference|color"/>
        </declare-styleable>
    </resources>

    2.新建类继承TextView。

    public class CustomRoundTextView extends AppCompatTextView {
        private  int solidColor;
        private  int stokeColor;
        private float stokeWidth;
        private  int cornerRadius;
        private Paint mPaint;
        public CustomRoundTextView(Context context) {
            super(context);
            init();
        }
    
    
    
        public CustomRoundTextView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.test);
            cornerRadius = typedArray.getInteger(R.styleable.test_radius, 20);
            stokeWidth = typedArray.getFloat(R.styleable.test_stokeWidth, 0.5f);
            solidColor = typedArray.getColor(R.styleable.test_solidColor, getResources().getColor(android.R.color.white,null));
            stokeColor = typedArray.getColor(R.styleable.test_stokeColor,solidColor );
    
            init();
        }
    
        public CustomRoundTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        private void init() {
            GradientDrawable gd = new GradientDrawable();
            gd.setCornerRadius(dip2px(cornerRadius));
            gd.setStroke(dip2px(stokeWidth), stokeColor);
            gd.setColor(solidColor);
            setBackgroundDrawable(gd);
        }
    
    
    
    
        public int dip2px(float dpValue) {
            final float scale = getResources().getDisplayMetrics().density;
            return (int) (dpValue * scale + 0.5f);
        }
    
    }

    3.xml文件中使用。

     <com.example.myapplication.CustomRoundTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:textSize="16sp"
            android:text="这是一个test"
            android:textColor="#000000"
            app:radius="5"
            app:stokeWidth="0.5"
            app:stokeColor="#000000"
            app:solidColor="#ff00000"
            />

    4.圆形自定义也可以拓展实现。

     效果实现如下:

    自定义view相关博客:

    1.Android自定义View全解

    2.自定义控件

    3.Android View的绘制流程

     

  • 相关阅读:
    任意文件读取与下载漏洞
    命令执行与代码执行漏洞原理
    安全面试知识汇总
    科学使用Github(gayhub)搜索想要的项目
    业务逻辑漏洞--注册-登录-改密码页面总结
    Linux提权之SUID提权
    攻防世界XCTF--伪造请求头XFF和Referer(第九题)
    攻防世界XCTF--两种传输方式get_post(第八题)
    攻防世界XCTF--一个简单的PHP后端验证(第七题)
    课时14.DTD文档声明上(掌握)
  • 原文地址:https://www.cnblogs.com/fangg/p/12747198.html
Copyright © 2011-2022 走看看