- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个自定义 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/
当软键盘出现在屏幕上时,我想使用 onSizeChange 来检测 LinearLayout 中的高度差。我想发布 fullScroll(View.FOCUS_DOWN);在那时候。一个例子将不胜感激
当用户触摸屏幕的某个部分时,是否会调用 onSizeChanged()? 我对 onDraw()、onMeasure() 和 onSizeChanged() 的调用顺序感到困惑。 最佳答案 Andro
这两个事件有什么区别?我想不出任何情况下两者都不会同时被调用,而且 MSDN 也没有启发性。 最佳答案 Answer from Bob Powell [MVP] found on internet (
我有一个以图形方式显示一些数据的应用程序。它创建了两个 View 来绘制图形并将它们添加到我的布局中。每个 View 以不同的方式显示数据,但每个 View 都以相同的方式实现 onSizeChang
我需要获取 View 的高度和宽度,并且我已经按照此处给出的示例对 View 进行子类化并使用 onSizeChanged(...)。但非常奇怪的是 onSizeChanged 给我正确的宽度,但高度
我无法正确调整元素(例如 ListView)的大小。 我有一个用于搜索的 ListActivity,带有仪表板、编辑框和 ListView 。这个想法是在键盘显示时隐藏操作栏。 我扩展了 Linear
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我创建了一个扩展 Drawable 的自定义类。我正在尝试使用 onSizeChanged() 获取 Drawable 的尺寸,如下所示 public class Circle extends Dra
我有一个 ListView ,它动态分配了不同的数据集。这很好用。我还使 fastScrollEnabled 为真。为了更新 sectionIndexer,我需要调用 list.setFastScro
我注意到在处理来自 Activity 的配置更改时,onLayout 和 onSizeChanged 在方向更改后立即连续调用 两次,从横向->纵向或从纵向->横向.此外,第一个 onLayout/o
我有一个在水平滚动窗口中按列显示文本和图像的 View 。它重排,因此(至少可能)随着内容的变化而调整自己的大小;当软键盘来来去去以及 View 高度发生变化时,它也会重新排列和调整大小(保持大致恒定
我的应用程序当前调用 onCreate() 方法中的一个方法,以便在创建 View 后游戏将启动并运行动画。但是,当我翻转屏幕以在纵向和横向之间切换时,会再次调用此方法。 我已将调用行移至 onSta
我有一个自定义 View ,里面只有一个圆圈。我对它的尺寸有疑问。 我覆盖了 onSizeChanged() 和 onMeasure(),但我担心 onMeasure() 遗漏了一些东西。这个 Vie
我正在编写一个自定义组件,一个强制内部 View 大小的 ViewGroup: myView.getLayoutParams().width = (int) itemSize; myView.getL
我是一名优秀的程序员,十分优秀!