2

Мне нужно построить макет, как на картинке ниже. Основная задача - центрировать текст.
Как центрировать Вид внутри макета

enter image description here

Я использую этот XML для реализации макета.

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

    <LinearLayout 
     android:id="@+id/ll" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center_vertical" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/author" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/author"> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/footer_layout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="bottom" 
     android:orientation="vertical" 
     android:layout_alignParentBottom="true"> 

     <LinearLayout 
      android:id="@+id/menu_layout" 
      android:layout_width="fill_parent" 
      android:layout_height="60dp" 
      android:background="@drawable/nav_overlay" 
      android:gravity="center_horizontal" 
      android:orientation="horizontal"> 

      <Button 
       android:id="@+id/previous" 
       android:layout_width="45dp" 
       android:layout_height="45dp" 
       android:background="@drawable/button" /> 

     </LinearLayout> 

     <com.google.ads.AdView 
      xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
      android:id="@+id/adView" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      ads:adSize="BANNER" 
      ads:adUnitId="@string/admob_id" 
      ads:loadAdOnCreate="true" /> 
    </LinearLayout> 

</RelativeLayout> 

Но он работает некорректно.
Мне нужно сосредоточиться TextViewandroid:id="@+id/author" внутри LinearLayoutandroid:id="@+id/ll". Но он сосредоточен в родителе RelativeLayoutandroid:id="@+id/main_layout".
Я использую android:layout_height="fill_parent" для LinearLayoutandroid:id="@+id/ll".
Как мой макет заполнить не родительский, а только оставшееся пространство?
Приведите пример своей реализации, если кто-нибудь придет к подобной проблеме.

EDIT: Теперь у меня неправильный текст с центром, как на картинке ниже.
enter image description here

В данный момент я считаю, что лучшим решением является использование android: layout_weight.
Но когда реклама не отображается, пропорции нарушаются.

+0

жаль , проверьте мой ответ еще раз. – goodm

+0

Даже при использовании 'android: layout_above:" @ + id/footer_layout "' still 'LinearLayout'' android: id = "@ + id/ll" 'растягивается до полного родителя' RelativeLayout' – dimetil

+0

Я только что проверил его и он работает отлично, я обновил свой ответ. – goodm

ответ

6

Попробуйте сейчас:

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

<LinearLayout 
    android:id="@+id/ll" 
    android:layout_above="@+id/footer_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_vertical" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/author" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/author"/> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/footer_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="bottom" 
    android:orientation="vertical" 
    android:layout_alignParentBottom="true"> 

    <LinearLayout 
     android:id="@+id/menu_layout" 
     android:layout_width="fill_parent" 
     android:layout_height="60dp" 
     android:background="@drawable/nav_overlay" 
     android:gravity="center_horizontal" 
     android:orientation="horizontal"> 

     <Button 
      android:id="@+id/previous" 
      android:layout_width="45dp" 
      android:layout_height="45dp" 
      android:background="@drawable/button" /> 

    </LinearLayout> 

    <com.google.ads.AdView 
     xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
     android:id="@+id/adView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     ads:adSize="BANNER" 
     ads:adUnitId="@string/admob_id" 
     ads:loadAdOnCreate="true" /> 
</LinearLayout> 

+0

Отличный ответ! Это действительно сосредоточено. Я пропустил что-то в первый раз. Благодарю. – dimetil

2

в LinearLayout, добавьте свойство: гравитация = "center_horizontal" и в TextView добавить свойство: layout_gravity = "center_horizontal"

+0

Я пытался это сделать. Но он сосредоточен в родительском RelativeLayout. Я хочу, чтобы Layout заполнил не родительский, а остальное пространство выше footer_layout. Спасибо, что пытались помочь. – dimetil

+0

попробуйте добавить вес к компонентам! –

+0

Хорошее предложение. Я попробовал. Но когда реклама не отображается - пропорции нарушаются. – dimetil

2

надстройку гравитацию к тексту. найти следующий код

 <TextView 
     android:id="@+id/author" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:text="hiiiiiiiiiii"/> 

это обеспечит ваш TextView в центре макета ...

0

Изменить макет вид текста, как следующее:

<TextView 
     android:id="@+id/author" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:text="@string/author"> 
    </TextView> 
</LinearLayout> 
0

набор layout_centerInParent = true

+0

Я добавил изображение для уточнения. – dimetil

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