2016-07-16 3 views
0

Я пытаюсь отобразить текстовое изображение в центре изображения, но не работает в LinearLayout.
Как выровнять TextView в центре изображения

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

<ImageView 
    android:id="@+id/kurtis" 
    android:layout_width="0dp" 
    android:layout_height="325dp" 
    android:layout_weight="0.5" 
    android:scaleType="fitXY" 
    android:src="@mipmap/kurtis" /> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_text="Testing" /> 

<ImageView 
    android:id="@+id/salwars" 
    android:layout_width="0dp" 
    android:layout_height="325dp" 
    android:layout_marginLeft="10dp" 
    android:layout_weight="0.5" 
    android:scaleType="fitXY" 
    android:src="@mipmap/salwar" /> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_text="Testing"/> 

Любой есть идея, пожалуйста, помогите мне.
Я отредактировал мой вопрос, это моя проблема.

+0

Я предлагаю вам использовать относительную компоновку, а затем, выровненный в layout_centerInParent. –

ответ

3

Можно, например, использовать RelativeLayout для центрирования TextView над ImageView:

<?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="wrap_content" 
    android:orientation="horizontal"> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="325dp" 
     android:layout_weight="0.5"> 

     <ImageView 
      android:id="@+id/kurtis" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="fitXY" 
      android:src="@mipmap/salwar"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_text="Testing"/> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="325dp" 
     android:layout_weight="0.5"> 

     <ImageView 
      android:id="@+id/salwars" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="fitXY" 
      android:src="@mipmap/salwar"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_text="Testing"/> 
    </RelativeLayout> 
</LinearLayout> 
+0

Это работает Спасибо – vinoth12594

+0

Добро пожаловать. Кроме того, вы должны поместить свое изображение в выпадающие папки, а не mipmap. Mipmap предназначен для значков запуска. – earthw0rmjim

1

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

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:baselineAligned="false" 
    android:orientation="horizontal"> 

    <FrameLayout 
     android:layout_width="0dp" 
     android:layout_height="325dp" 
     android:layout_marginRight="10dp" 
     android:layout_weight="1"> 

     <ImageView 
      android:id="@+id/kurtis" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginTop="10dp" 
      android:scaleType="fitXY" 
      android:src="@mipmap/kurtis" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:text="Testing" 
      android:textSize="18sp" /> 

    </FrameLayout> 

    <FrameLayout 
     android:layout_width="0dp" 
     android:layout_height="325dp" 
     android:layout_weight="1"> 

     <ImageView 
      android:id="@+id/salwar" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginTop="10dp" 
      android:scaleType="fitXY" 
      android:src="@mipmap/salwar" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:text="Testing" 
      android:textSize="18sp" /> 

    </FrameLayout> 

</LinearLayout> 
+0

Спасибо за ваш повтор – vinoth12594

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