Android - Set Layout_Gravity programmatically for LinearLayout

折月煮酒 提交于 2019-11-29 01:42:34

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.

Mohamed Abd Al-Mohsen

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!