zha*_*k86 4 android custom-view measure
我希望得到帮助.函数调用onMeasure我的问题.在My xml中有一个文件(RelativeLayput) - 3个元素: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");
}
}
Run Code Online (Sandbox Code Playgroud)
这里有什么问题.当应用程序启动时,会在onMeasure上完成,但为什么会这样做8次(在日志中可见).顺便说一下,如果要在其他图层中增加我的元素View的外壳,onMeasure它将执行2 ^(封闭图层的数量+2)时间.(即1为8,为2 - 16)此外,当我按下TextView事件时,onTouch被执行,但因此它也在onMeasure上启动.如何开始onMeasure不再是,并且,一般来说,它只启动了一次.我提前感谢你的帮助.
Rom*_*Guy 12
这种行为发生在两种情况下: - 当你使用一个LinearLayout并设置layout_weight其中一个孩子时.权重属性导致该孩子被测量两次.因此,如果你把另一个LinearLayout体重放在里面,你将得到4个叶子的措施.- 同样的事情可能发生在RelativeLayout.一些约束可能导致多次测量通过.
这意味着您应该注意权重属性和RelativeLayout.我建议您使用Android的traceview探查器和层次结构查看器来衡量布局的性能.