在onMeasue中处理自定义视图的权重

Chr*_*lde 6 android android-custom-view android-layout android-layout-weight

我有一个我正在创建的自定义视图,它将缩放其子项的字体大小TextViews以适应所有设置的宽度,因此每个都在它自己的行上.显然,它需要宽度来计算出来.我这样被覆盖了onMeasure():

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  int widthSize = MeasureSpec.getSize(widthMeasureSpec);
  int lineWidth = widthSize - getPaddingLeft() - getPaddingRight();

  // Change the text size until the largest line of text fits.
  while (lineWidth < calculateTextWidth()) {
    for (TextView textView : this.childViews) {
      float newSize = textView.getTextSize() - 1;
      textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, newSize);
    }
  }

  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
Run Code Online (Sandbox Code Playgroud)

calculateTextWidth()计算最大文本行的宽度,一切都适用.这段代码适用于FILL_PARENT和WRAP_CONTENT宽度,但是当我尝试给组件一个权重并让它自动设置它的权重时,它就会搞砸 - FILL_PARENT返回0,就像这样WRAP_CONTENT.这给出了一个很好的Activity Not Responding错误,因为lineWidth总是小于MeasureSpec.getSize(widthMeasureSpec)将返回的错误,因此0循环将永远运行.我使用的XML代码基本上是:

<com.my.widgets.ScalingTextViewGroup
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:orientation="vertical"
  android:gravity="center_horizontal"
  >
    <TextView
        android:id="@+id/text_1"
        android:text="Text 1"
        android:textSize="20sp" />
    <TextView
        android:id="@+id/text_2"
        android:text="Text 2"
        android:textSize="18sp" />
    <TextView
        android:id="@+id/text_3"
        android:text="Text 3"
        android:textSize="18sp" />
    <TextView
        android:id="@+id/text_4"
        android:text="Text 4"
        android:textSize="18sp" />
</com.my.widgets.ScalingTextViewGroup>
Run Code Online (Sandbox Code Playgroud)

我理解为什么它返回0 - 显然,它被设置为0 - 但我如何让它使用layout_weight?我觉得这应该有一个简单的答案,但我不知道它是什么.

Chr*_*lde 10

我最终通过一些重要的谷歌搜索搞清楚了.在这个页面上,我发现onMeasureandroid:layout_weight设置时实际上被调用了两次.我后来发现它在Android绘制视图中提到measure()可以被调用多次,它只是没有立即坐在我的大脑中.第一遍显然无法给出每个孩子的大小值,因为它不知道所有View的兄弟姐妹的权重是多少.它通过它一次,并使用MeasureSpec.UNSPECIFIED给出宽度为0,以查看子项是否有任何特定约束,然后再次通过MeasureSpec.EXACTLY分配实际权重.我的活动在第一次通过时搞砸了,所以它从未进入布局步骤.将onMeasure()修改为以下代码修复了问题.

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  if (widthMode != MeasureSpec.UNSPECIFIED) {
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int lineWidth = widthSize - getPaddingLeft() - getPaddingRight();

    // Change the text size until the largest line of text fits.
    while (lineWidth < calculateTextWidth()) {
      for (TextView textView : this.childViews) {
        float newSize = textView.getTextSize() - 1;
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, newSize);
      }
    }
  }

  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
Run Code Online (Sandbox Code Playgroud)