nuanchenmi 2018-12-20 09:28 采纳率: 0%
浏览 506

自定义TextView高度错误

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if(paint==null){
Log.d(TAG, "onMeasure: paint 初始化错误");
return;
}

    int heightMode =  MeasureSpec.getMode(heightMeasureSpec);
    int widthMode =  MeasureSpec.getMode(widthMeasureSpec);

    int height = MeasureSpec.getSize(widthMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);

    if(widthMode == MeasureSpec.AT_MOST){
        Rect bounds = new Rect();
        paint.getTextBounds(my_text,0,my_text.length(),bounds);
        width = bounds.width() + getPaddingLeft() +getPaddingRight();
    }

    if(heightMode == MeasureSpec.AT_MOST){
        Rect bounds = new Rect();
        paint.getTextBounds(my_text,0,my_text.length(),bounds);
        height = bounds.height() + getPaddingTop() +getPaddingBottom();
    }
    setMeasuredDimension(width,height);
}

    文本显示时,显示的内容只有基线以上的信息,基线以下不显示,比如y显示为v。但是看网友的代码高度都是bound.height(),直接加数值可以显示全部,但就不是自适应了,所以不知道怎么改
  • 写回答

1条回答 默认 最新

  • 就知道你会选我 2019-01-07 19:25
    关注

    继承自TextView,高度别自己计算,继承View岂不是还得自己计算换行?

    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题