Android: Center an image

前端 未结 13 1076
Happy的楠姐
Happy的楠姐 2020-12-07 17:38

I\'ve got a linear layout and an image...





        
13条回答
  •  自闭症患者
    2020-12-07 18:18

    Here are 2 ways you can center an image (or images) both vertically and horizontally in LinearLayout.

    (1) Use the android:layout_gravity="center" attribute in ImageView

     
        
    
    

    How this works: the android:layout_gravity="center" attribute in ImageView centers itself (i.e. the image) vertically and horizontally relative to its parent (LinearLayout).

    -- OR --

    (2) Alternatively, you can use the android:gravity="center" attribute in LinearLayout and omit the android:layout_gravity="center" attribute in ImageView :

     
        
    
    

    How this works: the android:gravity="center" attribute in LinearLayout centers its child/children (in this case, it's the image) vertically and horizontally.

提交回复
热议问题