Android: Center an image

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

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





        
相关标签:
13条回答
  • 2020-12-07 18:02

    What did it for me was

    LinearLayout
         RelativeLayout
              <ImageView
                  width... 
                  height..
         --->     android:layout_centerHorizontal="true"
    

    cheers!

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

    You can also use this,

    android:layout_centerHorizontal="true"
    

    The image will be placed at the center of the screen

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

    Another method. (in Relative tested, but I think in Linear would be also works)

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:adjustViewBounds="true"
        android:gravity="center"
    

    If you use Eclipse you can choose graphical layout when *.xml file is active. On the top, you will find the Structure and option Adjust View Bounds. It will cut short all dimensions of pseudo-frame (blue rectangle) to size of your drawable file.

    See also the scaleType option with make funny your image. Try it in Eclipse ;)

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

    This worked for me

    <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:contentDescription="@string/Logo"
                android:gravity="center"
                android:scaleType="centerInside"
                android:src="@drawable/logo" />
    
        </LinearLayout>
    
    0 讨论(0)
  • 2020-12-07 18:11

    First of all, you need to use 'match_parent' and don't use 'fill_parent' in the LinearLayout declaration, you could check the "official documentation" here https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#MATCH_PARENT Another observation is that you need to use the ImageView as a child, then use ImageView should be self-closing; this means that it should end with '/>'. Then let me show you some fast ideas: 1- As natural way

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

    Then looks like 2- You could improvise fastly

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    /><ImageView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:id="@+id/imageView1"
    android:scaleType="center"/><ImageView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:id="@+id/imageView1"
    android:src="@drawable/ocean"
    android:scaleType="center"/>
    <ImageView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:id="@+id/imageView1"
    android:background="#ffffff"
    android:scaleType="center"/></LinearLayout>
    

    And then looks like

    3- Other ideas could be born in that way, for instance this one

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    ><ImageView 
    android:layout_width="300dp" 
    android:layout_height="80dp"
    android:id="@+id/imageView1"
    /><ImageView 
    android:layout_width="300dp" 
    android:layout_height="80dp"
    android:id="@+id/imageView1"
    android:src="@drawable/ocean" 
    /> 
    <ImageView 
    android:layout_width="300dp" 
    android:layout_height="80dp"
    android:id="@+id/imageView1"
    /> </LinearLayout>
    

    Lokking like this

    Other possibility

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    ><ImageView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/ocean"
    android:id="@+id/imageView1"
    android:scaleType="center"/><ImageView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/imageView1"
    android:background="#ffffff"
    android:scaleType="center"/>
    <ImageView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:id="@+id/imageView1"
    android:background="#ffffff"
        android:src="@drawable/ocean"
    android:scaleType="center"/></LinearLayout>
    

    As we have been seeing, you could use 'hidden spaces' or 'spaces with background color' to fastly solve some trouble, obviously, this is not always a possibility

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

    In LinearLayout, use: android:layout_gravity="center".

    In RelativeLayout, use: android:layout_centerInParent="true".

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