Android: центрирование изображения - PullRequest
44 голосов
/ 23 мая 2011

У меня линейный макет и изображение ...

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

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

<ImageView android:layout_width="wrap_content" android:id="@+id/imageView1"
 android:src="@drawable/icon" android:layout_height="wrap_content"
 android:scaleType="center"></ImageView>

</LinearLayout>

Как динамически центрировать мое изображение так, чтобы оно отображалось в центре экрана на всех устройствах?

Ответы [ 12 ]

0 голосов
/ 18 апреля 2017

изменить вес макета в зависимости от того, что вы получите ....

Введите это:

 <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 голосов
/ 25 января 2015

попробуйте это.

<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>    
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...