zoukankan      html  css  js  c++  java
  • 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"
             />
    

      好了搞定了

  • 相关阅读:
    asp.net 内部重定向
    配置元素customErrors
    路由配置4-伪静态
    路由配置3
    路由配置2(转)
    ASP.NET实现图片防盗链(转)
    global中拦截404错误的实现方法
    使用一个HttpModule拦截Http请求,来检测页面刷新(F5或正常的请求)
    HttpApplication事件执行顺序(转)
    Android Studio NDK 新手教程(5)--Java对象的传递与改动
  • 原文地址:https://www.cnblogs.com/tony-yang-flutter/p/13810607.html
Copyright © 2011-2022 走看看