2014-12-21 2 views
0

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

enter image description here

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

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

<?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> 
+0

Посмотрите https://stackoverflow.com/a/39958875/3278589 – Subho

ответ

4

Вы можете использовать горизонтальную 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> 
+0

Можете ли вы изменить мой код пожалуйста? – Dev01

+0

Работает ли так, как вы хотите? – strings95

+0

Спасибо, что он сработал отлично :) – Dev01

1

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

Alternate SOLN с RelativeLayout есть в вашем ImageView тег использования имущества align_toLeftOf = «@ + идентификатор/R.id.Textview1"

И удалить центр в родителю из ImageView. Ваша проблема будет решена.

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

0

я привозила minel..just для простоты

<?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> 

с моим вам не придется беспокоиться о ImageView, вы просто расположить TextView в центре все в ..

+0

Вы помещаете прокладку и используете drawableLeft, что не является решением – Subho

Смежные вопросы