2017-01-14 2 views
1

У меня есть изображение и текстовое изображение внутри относительной компоновки. Изображение и текстовое изображение занимают 50% ширины экрана. Как я могу центрировать изображение и текстовое изображение? Я пробовал много методов, и я не знаю, что я делаю неправильно ...Центр ImageView в относительной компоновке

Вот мой код:

 <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="100"> 
     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="50" 
      android:layout_centerHorizontal="true"> 

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/textViewEventImage" 
       android:adjustViewBounds="true" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:background="@color/eventNameBackground" 
       android:id="@+id/textViewEventName" 
       android:textColor="@android:color/white" 
       android:textSize="15sp"/> 
     </RelativeLayout> 
    </LinearLayout> 

enter image description here

+0

ли вы хотите, чтобы ваше изображение будет в центре экрана? –

ответ

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:orientation="vertical" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.5" 
     android:background="#ff0000" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="180dp" 
     android:layout_height="120dp" 
     android:layout_centerInParent="true" 
     android:gravity="center_vertical" 
     android:src="@drawable/americanexpress" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_below="@+id/imageView1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="16dp" 
     android:text="Your product" /> 
</RelativeLayout> 

<View 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.5" 
    android:background="#00ff00" /> 

</LinearLayout> 
+0

Спасибо за ваш комментарий! :) –

0

Если вы хотите ImageView и TextView в центре , затем используйте это.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/textViewEventImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:src="@mipmap/ic_launcher" /> 

    <TextView 
     android:id="@+id/textViewEventName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textViewEventImage" 
     android:layout_centerInParent="true" 
     android:text="There is Your Text" 
     android:textSize="15sp" /> 
</RelativeLayout> 
1

Попробуйте как этот

Поместите imageview в

centerinparent ="true" 

textview И также centerinparent="true"

И добавьте один атрибут textview Как

android: layout_below="@+id/imageview id" 
+0

Thats does not work .. Изображение просматривается по-прежнему в левой части экрана, а textview находится в середине изображения. –

+0

Я добавил изображение к своему сообщению –

0

Может быть, это поможет

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true"> 
    <ImageView 
     android:layout_width="xxxdp" 
     android:layout_height="xxxdp" 
     android:src="@drawable/someimage" 
     /> 
    <TextView 
     android:layout_width="xxxdp" 
     android:layout_height="xxxdp" 
     android:text="fweqf"/> 
    </LinearLayout> 

</RelativeLayout> 
+0

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

+0

My ans будет давать результат в качестве изображения, которое вы указали, которое использует RelativeLayout, содержащий представление, которое представляет собой LinearLayout. И что вы подразумеваете под словом "text over imageview"? вы хотите, чтобы ваш образ выглядел как ваш текст? Мне бы хотелось узнать немного больше о том, что вы хотите сделать. – Simo