xml中为wrap_content,为什么会在 switch (widthMode)中出现两种情况?

wow334 2019-11-07 02:44:24
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">

<ddd.customview1.SimpleView2
android:layout_centerInParent="true"
android:id="@+id/progress"
app:progress="0"
app:bgcolor="@color/colorAccent"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

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

Log.v("haha--","00000000000");

switch (widthMode) {

case MeasureSpec.EXACTLY:
mWidth = 200;
mHeight = 30;
Log.v("haha--","2222222222");
break;

case MeasureSpec.AT_MOST:

mWidth = 400;
mHeight = 30;
Log.v("haha--","11111111111111");
break;

case MeasureSpec.UNSPECIFIED:
break;
}

setMeasuredDimension(mWidth,mHeight);

}

运行结果是: 11111111111111 之后 又打印了2222222

wrap_content不是只对应着 case MeasureSpec.AT_MOST: 这种情况吗?
...全文
126 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhaoqinmuxue 2019-11-09
  • 打赏
  • 举报
回复
说明调用了两遍测量方法,目测第一遍父view不知道大小,先测子view,测完得知父view大小。然后第二遍测量,用已知的父view大小再最终确定一遍子view的大小。
王能 2019-11-08
  • 打赏
  • 举报
回复
本来是自适应的,结果你measure时又固定宽度了,当然要再走一遍了
wow334 2019-11-07
  • 打赏
  • 举报
回复
代码见别人的博客https://blog.csdn.net/ZGUIZ/article/details/79319660
wow334 2019-11-07
  • 打赏
  • 举报
回复
自定义控件中的。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧