Android 我能';t更改ImageView的位置和TextView的setText
我有以下代码(适用于Android):Android 我能';t更改ImageView的位置和TextView的setText,android,Android,我有以下代码(适用于Android): 文本已设置,但未更改imageV的位置。当我不写text.setText(“Hi”),位置将被更改。我有一个绝对布局您需要使用设置布局参数 handler.post(new MyRunnable()); .... static public class MyRunnable implements Runnable { public void run() { text.setText("Hi"); final View
文本已设置,但未更改
imageV
的位置。当我不写text.setText(“Hi”)代码>,位置将被更改。我有一个绝对布局
您需要使用设置布局参数
handler.post(new MyRunnable());
....
static public class MyRunnable implements Runnable {
public void run() {
text.setText("Hi");
final ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) imageV.getLayoutParams();
params.left += y;
params.top = 218;
params.right = params.left + 150;
params.bottom = 464;
imageV.setLayoutParams(params);
}
}
请记住,在至少完成一次布局传递后,可以使用view.getLeft()
等等。在此之前,将返回0。如果你想通过布局来实现,那是不行的。您还必须调用requestLayout()。但是调用layout+requestLayout()与setLayoutParams的实现差不多,所以最好改用它。我不知道我是否正确,但当我启动事件管理器时,我会发现这个实现对于CPU来说更复杂(6-10x)(在我的程序中)。第二件事:我不明白为什么不能在Relative/abosolutelayout中使用setText和layout,我也尝试使用requestLayout()。因为我不想更改视图的边距,但我想设置视图的位置(x,y)。还有别的办法吗?谢谢你的帮助!更改视图位置和触摸区域(接收触摸EVNET的位置)的唯一方法是通过setLayoutParams。我知道的另一种方法是重写视图的onDraw/dispatchDraw方法,并在调用super.dispatchDraw()之前调用canvas.translate(x,y)
。然后,视图将被“移动”,但仅在视觉上移动。视图将继续在其旧位置(其左上边距的位置)接收触摸事件。为什么不愿意使用setLayoutParams?
handler.post(new MyRunnable());
....
static public class MyRunnable implements Runnable {
public void run() {
text.setText("Hi");
final ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) imageV.getLayoutParams();
params.left += y;
params.top = 218;
params.right = params.left + 150;
params.bottom = 464;
imageV.setLayoutParams(params);
}
}