
我的相对布局中的图片很少,如下所示.
<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;@OverrIDepublic 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);}解决方法:
您为相对布局创建新的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);祝好运…
总结以上是内存溢出为你收集整理的android-调整imageView的大小使其可以更改其位置全部内容,希望文章能够帮你解决android-调整imageView的大小使其可以更改其位置所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)