我想在用户触摸屏幕的位置添加新的图像视图。这是我的代码,但我看不到图像。
<TextView
android:layout_alignParentTop="true"
android:background="#AA000000"
android:textColor="#ffffffff"
android:gravity="center_vertical"
android:layout_gravity="top" android:text="Coordinate: 0x0" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/textView"></TextView>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/plane"/>
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textView = (TextView)findViewById(R.id.textView);
// this is the view on which you will listen for touch events
final View touchView = findViewById(R.id.LinearLayout01);
final View iv = findViewById(R.id.image);
touchView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// this is the image to display once on touch events.
iv.bringToFront();
int y = (int)event.getY();
int x = (int)event.getX();
textView.setText("Coordinates: " +
String.valueOf(x + "x" + String.valueOf(y)));
iv.layout(x, y, x, y);
return true;
}
});
}
答案 0 :(得分:0)
使用setText时会出现什么错误?
顺便说一句,如果可能的话,你可能想接受一些答案,因为它可以帮助你获得更多答案。