English 中文(简体)
如何在内部布局中中心视图
原标题:How to center View inside layout

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

""https://i.sstatic.net/gmuyX.jpg" alt="此处输入图像描述"/ >

我使用此 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>

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.

最佳回答

现在尝试 :

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

将重力添加到文本视图中。 查找以下代码

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

这将提供您在布局中心的文本视图...

更改文本视图布局如下 :

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

设置 layout_center_centerIn Patrent = true 设置 设置 layout_centerIn parrent = true





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...