对于继承View的自定义控件(下文件叫selfView),放在ScrollView里面或者继承它实现的自定义控件(下文统一叫selfScollView)里面onDraw实现是不会被调用的,selfView控件重写onMeasure方法,在重写的过程当中使用MeasureSpec此类来获取当前控件的高度时一直为零,无论自定义控件是否是指定了layout_heigth的具体值还是warp_context,match_parent。如果计算时height是零的情况下,控件不会显示。此时需要在调用的地方直接设置基高度,并调用自身的invalidate()然后使selfView重绘。
代码如下:
调用代码: override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_scoll) selftextview.setwidthheight(window.windowManager.defaultDisplay.width.toInt(),100); selftextview.invalidate() }
自定义控件:
class selfTextView :View { val paint=Paint(Color.RED) var txtstr :String="" val tag:String = "selftxt" constructor(context:Context):super(context) { Log.d(tag,"1") } constructor(context: Context,attributeSet: AttributeSet):super(context,attributeSet) { Log.d(tag,"2") var typedArray = context.obtainStyledAttributes(attributeSet,R.styleable.myScollView) txtstr = typedArray.getString(R.styleable.myScollView_txt) setWillNotDraw(false) paint.textSize = 20.0.toFloat() paint.color = Color.BLACK paint.style = Paint.Style.STROKE paint.strokeWidth = 1.0.toFloat() } var bcWidth:Int=0 var bcHeigth:Int = 0 open fun setwidthheight(width:Int,heigth:Int) { bcWidth = width bcHeigth = heigth } override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { super.onMeasure(widthMeasureSpec, heightMeasureSpec) if(bcWidth !=0 && bcHeigth!=0) { setMeasuredDimension(bcWidth,bcHeigth) }else { //处理warp_context 的支持 var widthMeasuremode = MeasureSpec.getMode(widthMeasureSpec) var widthMeasuresize = MeasureSpec.getSize(widthMeasureSpec) var heightMeasuremode = MeasureSpec.getMode(heightMeasureSpec) var heightMeasuresize = MeasureSpec.getSize(heightMeasureSpec) Log.d(tag, "onMeasure size " + widthMeasuresize.toString() + " " + heightMeasuresize.toString()) if (widthMeasuremode == MeasureSpec.AT_MOST && heightMeasuremode == MeasureSpec.AT_MOST) { Log.d(tag, "onMeasure 1") setMeasuredDimension(200, 200) } else if (widthMeasuremode == MeasureSpec.AT_MOST) { Log.d(tag, "onMeasure 2") setMeasuredDimension(200, heightMeasuresize) } else if (heightMeasuremode == MeasureSpec.AT_MOST) { Log.d(tag, "onMeasure 3") setMeasuredDimension(widthMeasuresize, 200) } else { Log.d(tag, "onMeasure 4") } } } override fun dispatchDraw(canvas: Canvas?) { super.dispatchDraw(canvas) Log.d(tag,"dispatchDraw") } override fun onDraw(canvas: Canvas?) { super.onDraw(canvas) Log.d(tag,"onDraw") var widthx = width - paddingLeft - paddingRight var heighty = height - paddingTop - paddingBottom if(txtstr.isEmpty()) { txtstr = "hello word" } Log.d(tag,txtstr) Log.d(tag,widthx.toString()) Log.d(tag,heighty.toString()) Log.d(tag,paddingLeft.toString()) Log.d(tag,paddingTop.toString()) canvas!!.drawText(txtstr,paddingLeft.toFloat(),30.0.toFloat(),paint) } }
布局文件:
<com.view.myScollView android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical"> <LinearLayout android:id="@+id/linelayoutcontext" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.selfview.selfTextView android:id="@+id/selftextview" android:layout_width="wrap_content" android:layout_height="100dp" android:background="@color/colorAccent" app:txt="ni hoa" /> </LinearLayout> </com.view.myScollView>
或者
<ScrollView android:layout_width="wrap_content" android:layout_height="match_parent"> <LinearLayout android:id="@+id/linelayoutcontexte" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.selfview.selfTextView android:id="@+id/selftextview2" android:layout_width="wrap_content" android:layout_height="100dp" android:background="@color/colorAccent" app:txt="ni hoa" /> </LinearLayout> </ScrollView>