Android - Set Layout_Gravity programmatically for LinearLayout

前端 未结 3 1140
执笔经年
执笔经年 2020-12-15 23:15

I\'ve got the following problem: I implemented a HorizontalScrollView which contains in one case a LinearLayout and an ImageView. In t

相关标签:
3条回答
  • 2020-12-16 00:08

    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);
    
    0 讨论(0)
  • 2020-12-16 00:09

    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);
    
    0 讨论(0)
  • 2020-12-16 00:15

    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.

    0 讨论(0)
提交回复
热议问题