我有一个自定义 View ,里面只有一个圆圈。 我对它的尺寸有疑问。
我覆盖了 onSizeChanged()
和 onMeasure()
,但我担心 onMeasure()
遗漏了一些东西。这个 View 基本上只是一个圆,我覆盖了 getSuggestedMinimumHeight()
和 getSuggestedMinimumWidth()
以使它们都返回圆的半径。现在一切似乎都运行良好,直到我决定更新该 Activity 的其他一些组件。
当我在我的 View 所在的 Activity 中更新 TextView
(或 Button
)的文本时,我的 View 变小(在每次更新时)同时调用onMeasure()
和 onSizeChanged()
。
这是我的onMeasure()
方法。我的问题是:为什么它的行为不总是一样?
@Override
protected int getSuggestedMinimumHeight() {
return (int) (mCircleRadius + mCircleStrokeWidth/2);
}
@Override
protected int getSuggestedMinimumWidth() {
return (int) (mCircleRadius + mCircleStrokeWidth/2);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.d("SIZES", "called onMeasure()");
int minw = getPaddingLeft() + getPaddingRight() + getSuggestedMinimumWidth();
int minh = getSuggestedMinimumHeight() + getPaddingBottom() + getPaddingTop();
setMeasuredDimension(minw, minh);
}
这是布局的 XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.nhasu"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<com.nhasu.ProgressCircleTimer
android:id="@+id/progcircle_smallBreaks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="10dp"
custom:circle_radius="300dp" />
<Button
android:id="@+id/btn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:onClick="onStartClicked"
android:text="@string/btn_start"
android:textSize="40sp" />
</RelativeLayout>
请注意,我使用简单的 Button.setText()
编辑了 Button
的文本。
最佳答案
您不需要 onSizeChanged
来确定自定义 View 的大小。当 onSizeChanged
被调用时,您的自定义 View 已被测量和布局。
只需适本地实现onMeasure
:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.d("SIZES", "called onMeasure()");
int minw = getPaddingLeft() + getPaddingRight() + getSuggestedMinimumWidth();
int minh = getSuggestedMinimumHeight() + getPaddingBottom() + getPaddingTop();
setMeasuredDimension(
resolveSize(minw, widthMeasureSpec),
resolveSize(minh, heightMeasureSpec));
}
public static int resolveSize(int childSize, int measureSpec) {
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
switch (specMode) {
case MeasureSpec.UNSPECIFIED:
// Just return the child's size.
return childSize;
case MeasureSpec.AT_MOST:
// Return the smallest of childSize and specSize
return (specSize < childSize)? specSize : childSize;
case MeasureSpec.EXACTLY:
// Should honor parent-View's request for the given size.
// If not desired, don't set the child's layout_width/layout_height to a fixed
// value (nor fill_parent in certain cases).
return specSize;
}
return childSize;
}
我在这个答案中添加了 resolveSize
的实现,这样您就可以看到发生了什么,但它已经在 View.resolveSize
中为您实现了。
关于android - 每次调用 onMeasure() 和 onSizeChanged() 时 View 变小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15095703/