【问题标题】:measure() doesnt work properly with dynamic layouts and textView - Androidmeasure() 不适用于动态布局和 textView - Android
【发布时间】:2011-07-22 07:28:24
【问题描述】:

我想将 RelativeLaout 视图转换为位图。我尝试了另一个答案和不同的选择,但没有成功。 我的 XML 视图是这样的:

----相对布局 --------ImageView --------文本视图

每个人都有 wrap_content 措施。

因为我需要几个视图的位图,我用这种方式膨胀它:

 LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看 v = inflater.inflate(R.layout.localviewlayout, null);

ImageView img = (ImageView) v.findViewById(R.id.imgPlace);
img.setImageBitmap(MyDynamicBitmap);

TextView txt1 = (TextView)v.findViewById(R.id.text1);
txt1.setText(MyDynamicText);
返回 v;

之后:

//在 measure() 中,我在 RelativeLayout.onMeasure() 上得到一个 nullpointerException。我也尝试过 //with MeasureSpec.EXACTLY v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 位图 b = Bitmap.createBitmap( v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888); v.draw(新画布(b)); 油漆 p = 新油漆(); myMainCanvas.drawBitmap(b, myPointX, myPointY, p);

RelativeLayout 的 textView 的内容是动态的,所以我无法知道文本的宽度或高度。 除了例外,如果我在测量函数上手动设置足够的大小(设置为 0),我的动态文本不会完全显示。 我希望你能帮助我,因为现在,我卡住了。谢谢你

【问题讨论】:

    标签: android dynamic bitmap textview measure


    【解决方案1】:

    尝试添加

    v.setLayoutParams(new LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    

    在致电measure() 之前。这是否修复了NullPointerException

    【讨论】:

    • 与 OP 有相同的问题,并从这篇文章中添加 setLayoutParams 修复了空指针异常。谢谢!
    • 谢谢!你救了我!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 2014-04-29
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多