【发布时间】:2012-08-15 05:41:08
【问题描述】:
我有一个相对布局,其中包含很少的图像,如下所示。
<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));
}
}
但我面临一个问题。 当我单击图像时,它的位置正在改变。 考虑一下这些屏幕截图..
以下是应用程序启动时的屏幕截图
以下是单击图像视图时的屏幕截图。
谁能给我建议,这样图片就不会改变它的位置 我可以使用诸如使用网格视图之类的东西(如果可能的话。但即使我正在尝试为其添加一些拖动选项。所以任何人都可以建议我)
更新代码:
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);
}
【问题讨论】: