2016-12-14 2 views
0

У меня есть LinearLayout, и я просто хочу поместить текст в середине моего изображения, которое является формой.Как централизовать текстовое изображение внутри изображения viewroid android

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

     <TextView 
      android:id="@+id/state_area" 
      android:layout_width="wrap_content" 
      android:textColor="#F000" 
      android:layout_height="90dp" 
      android:textSize="40sp" 
      android:layout_marginBottom="24dp" 
      android:layout_gravity="center_horizontal" /> 

    </LinearLayout> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/shape" 
     android:layout_gravity="center" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="24dp" 
     android:src="@drawable/shape" 
     android:layout_gravity="center" /> 

</LinearLayout> 

В результате до сих пор:

enter image description here

+0

Используйте FrameLayout обернуть два представления с ним –

ответ

1

Вы можете просто использовать текстовое изображение и задать форму в качестве фона.

<TextView 
     android:id="@+id/state_area" 
     android:layout_width="wrap_content" 
     android:textColor="#F000" 
     android:layout_height="90dp" 
     android:background="@drawable/shape" 
     android:textSize="40sp" 
     android:layout_marginBottom="24dp" 
     android:layout_gravity="center_horizontal" /> 
+0

почти там, но текст наверху, а не в центре –

+0

add gravity = center –

+0

Большое спасибо! –

1

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

+0

Относительная планировка будет беспорядок, я iwll попробовать этот TextView фон! –

1
Wrap your image and textview in framelayout 

<LinearLayout 
     android:layout_width="match_parent" 
     android:orientation="vertical" 
     android:layout_height="wrap_content"> 
     <FrameLayout android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <ImageView 

       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="24dp" 
       android:src="@drawable/shape" 
       android:layout_gravity="center" /> 
      <TextView 
       android:id="@+id/state_area" 
       android:layout_width="wrap_content" 
       android:textColor="#F000" 
       android:layout_height="90dp" 
       android:textSize="40sp" 
       android:gravity:"center" 
       android:layout_marginBottom="24dp" 
       android:layout_gravity="center" /> 
     </FrameLayout> 



     <FrameLayout android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="24dp" 
       android:src="@drawable/shape" 
       android:layout_gravity="center" /> 
      <TextView 
       android:id="@+id/state_area2" 
       android:layout_width="wrap_content" 
       android:textColor="#F000" 
       android:layout_height="90dp" 
       android:textSize="40sp" 
       android:gravity:"center" 
       android:layout_marginBottom="24dp" 
       android:layout_gravity="center" /> 
     </FrameLayout> 
    </LinearLayout> 
+0

почти там тоже, но дело в том, что текст не централизован, а да наверху –

+0

add gravity = "center" to textview – pipeur10

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