空中一点尘埃 2016-02-22 06:04 采纳率: 0%
浏览 2780

android测量控件的高度的问题

safe_content在布局中是高度是包裹内容
android:id="@+id/safe_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

在代码中
safe_content = (LinearLayout) view.findViewById(R.id.safe_content);
LayoutParams layoutParams = safe_content.getLayoutParams();
layoutParams.height = 0;
safe_content.setLayoutParams(layoutParams);

测量控件safe_content 高度
测量前
System.out.println("width====================="+safe_content.getMeasuredWidth());//等于344
System.out.println("height====================="+safe_content.getMeasuredHeight());//等于0
测量后
int widthMeasureSpec=MeasureSpec.makeMeasureSpec(MeasureSpec.EXACTLY, width);

int heightMeasureSpec=MeasureSpec.makeMeasureSpec(MeasureSpec.AT_MOST, 1000);//最大1000
safe_content.measure(widthMeasureSpec, heightMeasureSpec); // 通过该方法重新测量控件

System.out.println("width====================="+safe_content.getMeasuredWidth());//等于344
System.out.println("height====================="+safe_content.getMeasuredHeight());//等于135

那么问题来了,重新测量后为什么高度等于135,而不是0,它测的高度指的是什么
,不是当前设置layoutParams.height = 0;的高度吗,难道是在布局中 android:layout_height="wrap_content"的高度,测量控件测量的是什么的高度?

  • 写回答

1条回答

  • bdmh 移动开发领域优质创作者 2016-02-22 06:14
    关注

    measure后,会得到它实际在布局中的高度,所以对wrapcontent的,想要得到实际高度,要measure

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型