• Android可设置任意高度的TextView,如:设置0.5px设置0.1px等等


    一、概述

      今天UI给提了一个bug,说分割线的宽度有问题。必须要求线的高度为0.5px。但是layout_height只允许输入整数。想通过直接设置View高度的办法宣告凉凉的。

      然后突然想到Android绘图工具上可以把线的宽度设置成任意的。哎,还别说,这个办法还真行

    二、原理

      自定义一View,然后重写其onDraw方法,用canvas把相关的线线绘制出来。绘制出来的线可以是任意高度的,所以这种方案一下就满足了需求。下面看看具体代码怎样干(Kotlin写的)。如果是用Java的小伙伴,自行转换为Java代码。

    三、代码

    class ZeroDianFiveTextView : androidx.appcompat.widget.AppCompatTextView {
    
        private var lineHeight: Float = 0.25f
        private var lineColor: Int = resources.getColor(R.color.color_333333)
    
        constructor(context: Context) : super(context)
        constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
            val array = context.obtainStyledAttributes(attrs, R.styleable.ZeroDianFive)
            lineHeight = array.getFloat(R.styleable.ZeroDianFive_zeroLineHeight, 0.25f)
            lineColor = array.getColor(
                R.styleable.ZeroDianFive_zeroLineColor,
                resources.getColor(R.color.color_333333)
            )
        }
    
        override fun onDraw(canvas: Canvas?) {
            super.onDraw(canvas)
            val paint = Paint()
            paint.isAntiAlias = true;
            paint.strokeWidth = PxUtils.dp2px(context, lineHeight).toFloat()
            paint.color = lineColor
            canvas?.drawLine(0f, 0f, this.width.toFloat(), 0f, paint)
    
        }
    }
    

      

        <com.uns.uu.view.ZeroDianFiveTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:zeroLineColor="@color/B6B6B8"
            app:zeroLineHeight="0.3"
             />
    

      好了搞定了

  • 相关阅读:
    Python 3.4 .py文件打包成exe可执行文件方法
    windows找不到证书来让您登陆到网络,启用IEEE 802.1X验证为灰色
    重装系统之后电脑配置步骤
    win7(x64)matlab2010a 编译器安装
    用Interface Builder自定义UITableViewCell
    ASIHTTPRequest类库简介和使用说明
    IOS NSURL基本操作
    arc下asihttprequest等应用实现
    WampServer的配置
    javascript带范围的随机整数生成22
  • 原文地址:https://www.cnblogs.com/tony-yang-flutter/p/13810607.html
Copyright © 2020-2023  润新知