如何从XML中调整ImageView的大小?

时间:2015-06-12 03:14:04

标签: android imageview

我希望在Java Activty中从XML调整ImageView的高度和宽度(如何进行调整?

private ImageView imageView;
imageView = (ImageView) findViewById(R.id.imageView);

2 个答案:

答案 0 :(得分:0)

通过 xml 设置宽度和高度:

<ImageView
   android:id="@+id/imgLauncher"
   android:layout_width="50dip"
   android:layout_height="50dip"
   android:src="@drawable/ic_launcher" />

您必须根据您的要求更改 android:layout_width =“” android:layout_height =“”

设置宽度和高度 Java 代码:

private ImageView imgLauncher;

绑定视图

imgLauncher = (ImageView)findViewById(R.id.imgLauncher);

通过 LayoutParams

设置 Widht Heigth
imgLauncher.getLayoutParams().height = 20;
imgLauncher.getLayoutParams().width = 20;
设置 LayoutParams

后,

刷新视图

imgLauncher.requestLayout();

完成

答案 1 :(得分:0)

只需在活动中添加代码

即可
Display display = getWindowManager().getDefaultDisplay();
ImageView iv = (LinearLayout) findViewById(R.id.ivMyImage);
int width = 30; 
int height = 30;
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
iv.setLayoutParams(parms);