<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" tools:context="${relativePackage}.${activityClass}" > <ImageView android:id="@+id/image01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitXY" android:src="@drawable/aabb" /></RelativeLayout>
public
class
MainActivity
extends
Activity {
private
ImageView image;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView)findViewById(R.id.image01);
//设置图片的位置
MarginLayoutParams margin9 =
new
MarginLayoutParams(
image.getLayoutParams());
margin9.setMargins(
400
,
10
,
0
,
0
);
//在左边距400像素,顶边距10像素的位置显示图片
RelativeLayout.LayoutParams layoutParams9 =
new
RelativeLayout.LayoutParams(margin9);
layoutParams9.height =
600
;
//设置图片的高度
layoutParams9.width =
800
;
//设置图片的宽度
image.setLayoutParams(layoutParams9);
}
}
Android代码中动态设置图片的大小(自动缩放),位置
最新推荐文章于 2021-05-26 14:31:39 发布