我希望得到帮助。我的问题与函数调用onMeasure有关。在我的xml文件中(RelativeLayout),我有三个元素:TextView,RelativeLayout和在其中包含我的类。现在我的主要Activity类和我的子类如下:
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次(可以从日志中看到)。顺便说一下,如果在其他层中增加我的View元素的包围盒,onMeasure将会执行2 ^(封闭层数量+2)次。(即对于一个是8,对于2是16)接下来,当我按下TextView时,触摸事件被执行,但同时也启动了onMeasure。如何使得onMeasure不再启动,并且总体上只启动一次。提前感谢您的帮助。