调整imageView的大小会导致其位置改变。

3

我有一个类似下面这样的相对布局,其中包含一些图片。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >


    <ImageView
        android:id="@+id/image_2"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignLeft="@+id/increase"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="34dp"
        android:layout_marginTop="34dp"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/image_1"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignBottom="@+id/image_2"
        android:layout_marginBottom="14dp"
        android:layout_toRightOf="@+id/increase"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/image_8"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />
</RelativeLayout>

在我的Java文件中,我需要执行一个小任务。当我点击一个图像时,它的大小应该增加。我尝试过这个,并且它是有效的,这是我的代码:

public class MainActivity extends Activity implements OnClickListener {

View imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageView i1 = (ImageView ) findViewById(R.id.image_1);
    ImageView i2 = (ImageView ) findViewById(R.id.image_2);
             ImageView i3 = (ImageView ) findViewById(R.id.image_8);
            i1.setOnClickListener(this);
    i2.setOnClickListener(this);
    i3.setOnClickListener(this);
}

public void onClick(View arg0) {
    Log.v("clicked ", "clicked");
    imageView = arg0;
    Toast.makeText(this, "looking to resize the image ", 1).show();
    Toast.makeText(this, "clicked 2", 1).show();

    height = imageView.getHeight();
        width = imageView.getWidth();
    height += 50;
    width += 50;
    imageView.setLayoutParams(new RelativeLayout.LayoutParams(height, width));

}   

}

但我遇到了一个问题。当我点击图片时,它的位置会改变。请看下面的屏幕截图...

下面是应用程序启动时的屏幕截图:

enter image description here

下面是当图像视图被点击时的屏幕截图。

enter image description here

有人能建议一下,这样图片就不会改变位置。我可以尝试使用像“网格视图”这样的东西(如果可能的话),但我也在尝试添加一些拖动选项。所以有人能给我建议吗?

更新后的代码:

onclick方法更新如下:

public void onClick(View arg0) {
    Log.v("clicked ", "clicked");
    imageView = arg0;
    Toast.makeText(this, "looking to resize the image ", 1).show();
    Toast.makeText(this, "clicked 2", 1).show();

    height = imageView.getHeight();
width = imageView.getWidth();
    height += 50;
    width += 50;
    RelativeLayout.LayoutParams params = new
          RelativeLayout.LayoutParams(height, width);

    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    params.setMargins( 
                imageView.getLeft()-30,imageView.getTop()-30,imageView.getRight()+30,
               imageView.getBottom()+30);
    imageView.setLayoutParams(params);



}
1个回答

5
你需要为相对布局创建新的layoutparams,并仅设置高度和宽度。因此,图像视图将默认位于父相对布局的左上角。您还需要根据需要设置对齐和边距。
由于您正在使用相对布局,因此需要创建新的RelativeLayout layoutParams:

http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

类似这样:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
params.setMargins(left, top, right, bottom);

祝你好运...


1
addRule使您能够在父视图中指定子视图的“相对位置”,例如对齐和“toTheRightOf”等。当要求相对布局绘制自身时,它首先要求层次结构中的所有子项报告其大小和所需位置。为此,它向下传递布局用于定位其子项的规则。请记住,在相对布局中,除了左上角,您必须提供规则来定位其他任何内容。一般来说,边距不是做这件事的好方法,但有时候,您无法避免使用它。 - Simon
有人能再帮我一下吗? 我没有找到这个问题的解决方案。 - G_S
1
很遗憾,“我没有找到解决方案”这样的说法并不能让任何人提供帮助,无论他们的技术知识有多深,或者他们有多想帮忙。你需要学会提出具体的问题,并提供足够的信息,以便我们能够提供帮助。你可能会发现这篇文章很有用,而且篇幅很短:http://stackoverflow.com/questions/how-to-ask - Excrubulent
它应该是params.setMargins(left,top,right,bottom); - samgak
@samgak 很好的发现!谢谢。 - Simon
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接