Подтвердить что ты не робот

Android: как выровнять изображение в горизонтальном центре изображения?

Я пробовал все типы scaletypes, но все они приводят к тому, что изображение находится в левом углу изображения.

 <ImageView
    android:id="@+id/image"
    android:scaleType="centerInside"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_marginRight="6dip"
    android:background="#0000" 
    android:src="@drawable/icon1" />
4b9b3361

Ответ 1

В вашем ImageView есть атрибут wrap_content. Я бы подумал, что изображение сосредоточено внутри изображения, но сам образ не центрируется в родительском представлении. Если на экране есть только изображение, попробуйте match_parent вместо wrap_content. Если в макете имеется более одного вида, вы должны центрировать изображение.

Ответ 2

<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>;

Ответ 3

Вы можете центрировать сам ImageView, используя:

android:layout_centerVertical="true" или android:layout_centerHorizontal="true" Для вертикального или горизонтального. Или центрировать внутри родителя:

android:layout_centerInParent="true"

Но похоже, что вы пытаетесь центрировать фактическое исходное изображение внутри самого изображения, о котором я не уверен.

Ответ 4

Это работает для меня при выравнивании изображения в режиме linearlayout.

<ImageView android:id="@android:id/empty"
    android:src="@drawable/find_icon"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:scaleType="center"
    />

Ответ 5

Попробуйте этот код:

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal">

            <ImageView
                android:id="@+id/imgBusiness"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:src="@drawable/back_detail" />
</LinearLayout>

Ответ 6

Для меня android:gravity="center" сделал трюк в родительском элементе макета.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/fullImageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:contentDescription="@string/fullImageView"
        android:layout_gravity="center" />

</LinearLayout>

Ответ 7

Использование только "fill_parent" для layout_width сделает трюк:

 <ImageView
    android:id="@+id/image"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="6dip"
    android:background="#0000" 
    android:src="@drawable/icon1" />

Ответ 8

Try:

android:layout_height="wrap_content"
android:scaleType="fitStart"

на изображении в RelativeLayout

Ответ 9

Используйте этот атрибут: андроид: layout_centerHorizontal = "истина"

Ответ 10

Это код в xml, как центрировать ImageView, я использовал "layout_centerHorizontal".

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_centerHorizontal="true"
    >
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/img2"
    />
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/img1"
        />
</LinearLayout>

или этот другой пример...

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    >
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/img2"
    />
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/img1"
        />
</LinearLayout>