代码之家  ›  专栏  ›  技术社区  ›  OiRc

更改图像视图的位置

  •  12
  • OiRc  · 技术社区  · 11 年前

    我无法更改登录按钮的位置。因为当我尝试时,它无法从原始位置移动。我已经设置了 android:layout_gravity android: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 回复  |  直到 11 年前
        1
  •  18
  •   NayMak    8 年前

    您不能在xml本身中执行此操作,您需要创建一个实例 ImageView 在您的活动中调用 setX() setY() 方法设置坐标。

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

    示例:

    ImageView s = (ImageView) findViewById(R.id.your_id);
    s.setY(number);
    s.setX(number);
    
        2
  •  4
  •   Vijay Barbhaya    11 年前
    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);