改变图像视图的位置

时间:2014-08-10 08:05:35

标签: android imageview

我无法更改登录按钮的位置。因为当我尝试时,它无法从原始位置移动。我设置了android:layout_gravityandroid:gravity,但我希望它处于特定的位置。

我如何设置图像的坐标(x,y)?

这是login_fragment.xml

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical" 
<ImageView
    android:id="@+id/login"
    android:layout_width="400px"
    android:layout_height="100px"
    android:src="@drawable/login_button" />

2 个答案:

答案 0 :(得分:15)

您无法在xml内部执行此操作,您需要在活动中创建ImageView的实例,并调用其setX()setY()方法来设置坐标。

小心每个屏幕都有不同的像素数,您可能在不同的设备上有不同的结果。

<强>示例:

ImageView s = (ImageView) findViewById(R.id.your_id);
s.setY(number);
s.setX(number);

答案 1 :(得分:3)

TranslateAnimation animation = new TranslateAnimation(0.0f, 50.0f, 0.0f, 0.0f);
animation.setDuration(700);
animation.setRepeatCount(5);
animation.setRepeatMode(2);
animation.setFillAfter(true);
image.startAnimation(animation);
相关问题