我的布局中有一个自定义视图,布局中还有一个 TextView,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_alignParentTop="true" android:layout_centerInParent="true"
android:text="Tiny" android:orientation="vertical">
<my.package.drawView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView android:text="Application Name" android:id="@+id/appName"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="30px" android:textStyle="italic" />
</LinerLayout>
我在活动的 onCreate 方法和自定义视图的 onDraw 方法中设置了对 TextView 的引用,我将 TextView 的文本更改如下:
public void onDraw(Canvas canvas) {
tv.setText("player score: "+score);
}
但是在 ondraw 方法中设置文本会导致它进入无限循环。有人可以告诉我如何在调用自定义视图的 onDraw 方法中或之后更新 TextView 的文本。