Центрировать ImageView и TextView рядом

Я хочу центрировать весь макет содержимого по центру, но это проблема, с которой я столкнулся:

введите здесь описание изображения

Как видите, я хочу центрировать изображение и текст в их родителях, но хочу, чтобы изображение было слева от текста.

Вот код макета:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/app_light_green" >

    <ImageView
        android:id="@+id/toastImage"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_centerInParent="true"
        android:paddingTop="1dp"
        android:src="@drawable/exclamation" />

    <TextView
        android:id="@+id/toastText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:paddingBottom="3dp"
        android:paddingLeft="5dp"
        android:paddingRight="3dp"
        android:paddingTop="2dp"
        android:textColor="@color/app_darker_green"
        android:textSize="@dimen/sixteen_sp" />

</RelativeLayout>

person Dev01    schedule 21.12.2014    source источник
comment
Взгляните на stackoverflow.com/a/39958875/3278589.   -  person Subho    schedule 05.02.2018


Ответы (3)


Вы можете использовать горизонтальный Linearlayout и добавить в него TextView и ImageView, а затем добавить Linearlayout в Relativelayout и сделать его центральным.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/toastImage"
        android:layout_width="24dp"
        android:src="@drawable/exclamation"
        android:layout_height="24dp"
        android:paddingTop="1dp" />

    <TextView
        android:id="@+id/toastText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingBottom="3dp"
        android:paddingLeft="5dp"
        android:paddingRight="3dp"
        android:paddingTop="2dp"
        android:textColor="@color/app_darker_green"
        android:textSize="@dimen/sixteen_sp"/>
</LinearLayout>

</RelativeLayout>
person strings95    schedule 21.12.2014
comment
Без рассказа об изображении посмотрите stackoverflow.com/a/39958875/3278589 - person Subho; 05.02.2018

Попробуйте использовать фреймворк. В макете кадра добавьте изображение и текст, назначьте гравитацию, чтобы она оставалась для изображения, а гравитация была в центре для просмотра текста. И, наконец, дайте некоторое левое поле для просмотра изображения, чтобы выровнять его с текстовым представлением.

Альтернативное решение с относительным расположением: в вашем теге изображения используйте свойство align_toLeftOf="@+id/R.id.Textview1"

И удалите центр родителя из изображения. Ваша проблема будет решена.

Пожалуйста, игнорируйте синтаксис, который я набрал с телефона. :)

person Udit Kapahi    schedule 21.12.2014

я также приношу минель..просто для простоты

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/app_light_green" >


<TextView
    android:id="@+id/toastText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:paddingBottom="3dp"
    android:paddingLeft="5dp"
    android:drawableLeft="@drawable/exclamation"
    android:paddingRight="3dp"
    android:paddingTop="2dp"
    android:textColor="@color/app_darker_green"
    android:textSize="@dimen/sixteen_sp" />

</RelativeLayout>

с моим вам не нужно беспокоиться о просмотре изображения, вы просто позиционируете свой текст в центре, вот и все.

person Elltz    schedule 21.12.2014
comment
Вы добавляете отступы и используете drawableLeft, что не является решением - person Subho; 05.02.2018