سؤال

I have to build layout like at the picture below. The main task is to center the text.

enter image description here

I use this xml for implementing layout.

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

But it works incorrectly.
I need to center TextView android:id="@+id/author" inside LinearLayout android:id="@+id/ll". But it is centered in the parent RelativeLayout android:id="@+id/main_layout".
I use android:layout_height="fill_parent" for LinearLayout android:id="@+id/ll".
How can my Layout fill not the parent but only remaining space?
Give an example of its implementation, if anyone come to similar issue.

EDIT: Now I have wrong centered text like at the picture below now.
enter image description here

At this moment, I think the best solution is to use android:layout_weight.
But when the ads are not shown - the proportions are violated.

هل كانت مفيدة؟

المحلول

Try now:

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

نصائح أخرى

in the linearlayout, add the property: gravity="center_horizontal" and in the textview add the property: layout_gravity="center_horizontal"

add gravity to your textview. find the following code

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

this will provide your textview in the centre of the layout...

Change your text view layout like following :

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

set layout_centerInParent = true

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top