我希望得到帮助。我对函数调用 onMeasure 的问题。在我的 xml 中,一个文件(RelativeLayput)- 3 个元素:TextView、RelativeLayout 并将它们包含在我的类中。现在我的主要活动类和我的创建类
public class MainActivity extends Activity implements OnTouchListener {
final String LOG_TAG = "myLogs";
TextView tv1;
UiMyClass uimc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView) findViewById(R.id.mytext2);
tv1.setOnTouchListener(this);
String str1 = "qwertyy";
uimc = (UiMyClass) findViewById(R.id.myclass1);
Log.d(LOG_TAG, "Open1-0" + " " + str1);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int evX = (int) event.getX();
int evY = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
tv1.setText("uyq8qyw");
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
Log.d(LOG_TAG, "Touch_GS");
return true;
}
}
public class UiMyClass extends View {
final String LOG_TAG = "myLogs";
public UiMyClass(Context context, AttributeSet attrs) {
super(context, attrs);
this.setBackgroundResource(R.drawable.ic_launcher);
Log.d(LOG_TAG, "Initialization_00");
}
@Override
protected void onMeasure(int x, int y) {
setMeasuredDimension(100, 50);
Log.d(LOG_TAG, "Measure_02");
}
}
这里有什么问题。当应用程序启动时,执行 onMeasure,但为什么会执行 8 次(在日志中可见)。顺便说一句,如果要在其他层增加我的元素视图的包围,onMeasure它执行2 ^(包围层的数量+2)时间。(即一个8,2 - 16) 此外,当我按下TextView 事件onTouch 执行,但也因此它启动onMeasure。如何使 onMeasure 不再启动,一般来说,它只启动一次。在此先感谢您的帮助。