Android: Center an image

前端 未结 13 1031
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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"> 
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            layout_height="wrap_content"
            android:src="@drawable/icon"
            android:layout_gravity="center"/>
    </LinearLayout>
    

    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 :

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"> 
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            layout_height="wrap_content"
            android:src="@drawable/icon"
            android:layout_gravity="center"/>
    </LinearLayout>
    

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

    0 讨论(0)
  • 2020-12-07 18:20

    Simply add this to your ImageView.

    android:layout_gravity="center" 
    
    0 讨论(0)
  • 2020-12-07 18:22

    android:scaleType ="centerInside" --> this line use to center an image,, but you need to keep height and width of an image as wrap_context

     <ImageView
                android:id ="@+id/imageView6"
                android:layout_width ="wrap_content"
                android:layout_height ="wrap_content"
                android:scaleType ="centerInside"
                android:layout_marginTop ="20dp"
                app:srcCompat ="@drawable/myimage" />
    
    0 讨论(0)
  • 2020-12-07 18:22

    change layout weight according you will get....

    Enter this:

     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0.03">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:scaleType="centerInside"
            android:layout_gravity="center"
            android:src="@drawable/logo" />
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-07 18:23

    try this.

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:orientation="horizontal" >
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:scaleType="centerInside"
            android:src="@drawable/logo" />
    
    </LinearLayout>    
    
    0 讨论(0)
  • 2020-12-07 18:25

    If you are using a LinearLayout , use the gravity attribute :

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
    
        <ImageView android:layout_width="wrap_content"
            android:id="@+id/imageView1"
            android:src="@drawable/icon"
            android:layout_height="wrap_content"
            android:scaleType="centerInside" />
    </LinearLayout>
    

    If you are using a RelativeLayout , you can use android:layout_centerInParent as follows :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <ImageView android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon"
            android:scaleType="centerInside"
            android:layout_centerInParent="true" />
    
    </RelativeLayout>
    
    0 讨论(0)
提交回复
热议问题