ScrollView中自定义View不显示
2021/4/29 18:56:51
本文主要是介绍ScrollView中自定义View不显示,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
public class TextCustomView extends View { public TextCustomView(Context context) { super(context); init(context); } public TextCustomView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(context); } private Paint paint; private void init(Context context) { paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.parseColor("#00008a")); paint.setTextSize(BaseUtils.dip2px(context, 16)); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.parseColor("#ffebcc"));//来个背景色方便看控件大小 String txt="换行必须用StaticLayout,drawText只显示一行,不信你看"; canvas.drawText(txt,10,400,paint); } }
放在LinearLayout中,显示没问题。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical" tools:context=".InputActivity"> <com.lagou.test.view.input.TextCustomView android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
然后LinearLayout换成ScrollView之后 不显示了。
重写noMeasure方法:
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); if (MeasureSpec.UNSPECIFIED == heightMode) { int heightSize = (int) (widthSize * 1.2f); setMeasuredDimension(widthMeasureSpec, heightSize); } }
这样就可以重新显示了。
原因是ScrollView的onMeasure并未处理MeasureSpec.UNSPECIFIED。
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (!mFillViewport) { return; } final int heightMode = MeasureSpec.getMode(heightMeasureSpec); if (heightMode == MeasureSpec.UNSPECIFIED) { return; } ....... ....... }
父布局遇到MeasureSpec.UNSPECIFIED 直接return,导致子控件默认的测量方式得出的高度就是 初始值 0,所以不显示。此时需要子控件自己处理该case。
上面重写onMeasure可以优化为这样:
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int reHeight = resolveSize(heightSize, heightMeasureSpec); setMeasuredDimension(widthMeasureSpec, reHeight); } }
resolveSize源码处理了 case MeasureSpec.UNSPECIFIED:
,源码如下
public static int resolveSize(int size, int measureSpec) { return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK; } public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) { final int specMode = MeasureSpec.getMode(measureSpec); final int specSize = MeasureSpec.getSize(measureSpec); final int result; switch (specMode) { case MeasureSpec.AT_MOST: if (specSize < size) { result = specSize | MEASURED_STATE_TOO_SMALL; } else { result = size; } break; case MeasureSpec.EXACTLY: result = specSize; break; case MeasureSpec.UNSPECIFIED: default: result = size; } return result | (childMeasuredState & MEASURED_STATE_MASK); }
这篇关于ScrollView中自定义View不显示的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-07Dify + TiDB Vector,快速构建你的AI Agent
- 2024-07-06有没有什么开源的py项目可以对图像进行分类-icode9专业技术文章分享
- 2024-07-05feign默认connecttimeout和readtimeout是多少-icode9专业技术文章分享
- 2024-07-05idea控制台,日志太多,导致部分想看得日志被刷走 搜不到-icode9专业技术文章分享
- 2024-07-05The server selected protocol version Tls10 is not accepted by client preferences [TLs12]-icode9专业技术文章分享
- 2024-07-05怎么清理项目缓存-icode9专业技术文章分享
- 2024-07-04安装 Eyoucms详细图文教程-icode9专业技术文章分享
- 2024-07-04ueditor 复制文章时,图片的链接是一个下载图片地址,该如何处理?-icode9专业技术文章分享
- 2024-07-04怎样判断host有没有对wordpress有缓存呢-icode9专业技术文章分享
- 2024-07-04具有编译功能的系统make后,无法ssh连接-icode9专业技术文章分享