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

Как центрировать ImageView в LinearLayout?

Следующий код XML не работает, чтобы центрировать ImageView в LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/myimg" 
        android:layout_width="36dip"
        android:layout_height="36dip"
        android:scaleType="fitXY"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/my_background"
    />
</LinearLayout>

И это код RelativeLayout, который неожиданно работает:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/myimg" 
        android:layout_width="36dip"
        android:layout_height="36dip"
        android:scaleType="fitXY"
        android:layout_centerHorizontal="true"
        android:background="@drawable/my_background"
    />
</RelativeLayout>

Может кто-нибудь сказать мне, почему? Спасибо!

4b9b3361

Ответ 1

Вам нужно установить вертикальную ориентацию на LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
    android:id="@+id/myimg" 
    android:layout_width="36dip"
    android:layout_height="36dip"
    android:scaleType="fitXY"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/my_background"
/>

Ответ 2

Извините, но выше код не работал у меня, поэтому я отправляю свой код, который работал у меня,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical">

   <ImageView
        android:id="@+id/ivPic"
        android:layout_width="70dp"
        android:layout_height="70dp" />

</LinearLayout>

Я надеюсь, что это поможет другим, кто нуждается.

Ответ 3

Что обычно работает для меня, я обертываю изображение в RelativeLayout, а затем переходит в LinearLayout, убедившись, что атрибуты высоты и ширины relativelayout соответствуют линейномуLayout. И вы знаете, как сохранить изображение в центре релятивирования.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
              android:layout_width="fill_parent"
              android:layout_height="wrap_content">
              <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                         <ImageView 
                             android:id="@+id/myimg" 
                             android:layout_width="36dip"
                             android:layout_height="36dip"
                             android:scaleType="fitXY"
                             android:orientation="vertical"
                             android:layout_gravity="center_horizontal"
                             android:background="@drawable/my_background"
                         />
              </RelativeLayout>
    </LinearLayout>

Ответ 4

Проблема заключается в том, что вы используете android: layout_width = "fill_parent" в LinearLayout. Измените его на "wrap_content"

Ответ 5

<LinearLayout
    android:orientation="horizontal"  
    android:layout_width="0dp"
    android:layout_weight="0.50"
    android:gravity="center"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/addToDateOrReceipt"
        android:layout_width="25dp"
        android:layout_height="25dp" />
</LinearLayout>

Установите свойство android: gravity вашего LineraLayout в центр.

Ответ 6

Вы также можете сделать это с помощью FrameLayout и внутри него LinearLayout, поэтому его проще управлять и проще размещать в центре представления.

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical">
 <LinearLayout
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="center"
     android:orientation="vertical">

     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/app_name" />

     <ImageView
         android:id="@+id/ivPic"
         android:layout_width="70dp"
         android:layout_height="70dp" />
 </LinearLayout>
</FrameLayout>

Я знаю, что вы спрашиваете о том, как сосредоточить изображение в макете, но хотите предоставить другой способ сделать это.