调整 imageView 的大小会使其改变位置

问题描述 投票:0回答:1

我有一个相对布局,其中很少有图像,如下所示。

<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);



}
android android-layout
1个回答
5
投票

您为相对布局创建新的布局参数,并且仅设置高度和宽度。因此,图像视图将默认位于父相对布局的左上角。您还需要根据需要设置对齐方式和边距。

由于您使用的是相对布局,因此需要创建新的RelativeLayout布局参数:

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);

祝你好运...

© www.soinside.com 2019 - 2024. All rights reserved.