Android - Set Layout_Gravity programmatically for LinearLayout
Do somethings like this :
LayoutParams lp = new LayoutParams(); lp.gravity= Gravity.CENTER_HORIZONTAL; myImg.setLayoutParams(lp);
UPDATE :Another way to do this :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.weight = 1.0f; params.gravity = Gravity.CENTER; imageView.setLayoutParams(params);
Put the ImageView
in a FrameLayout
like this:
Removed extra code
ImageView image = new ImageView(mContext);image.setLayoutParams(new FrameLayout.LayoutParams(width, height, gravity));
More info here.
I think , this one will work for you
FrameLayout.LayoutParams layoutParams = new ScrollView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams1.gravity(YourGravity);yourImage.setLayoutParams(layoutParams);