android 使用Activity类布局时怎样让图片居中

1、通过xml布局显示,在加载图片的View加入以下属性让整个View居中
a、线性布局(LinearLayout)
android:layout_gravity=”center”
b、相对布局(RelativeLayout)
android:layout_centerInParent=”true”
2、通过java代码动态实现
a、线性布局(LinearLayout)
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
view.setLayoutParams(params);
b、相对布局(RelativeLayout)
RelativeLayout.LayoutParams params = new RelativeLayout().LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE);
view.setLayoutParams(params);

你可能感兴趣的:(android 使用Activity类布局时怎样让图片居中)