奇怪的布局Android

时间:2015-11-05 14:07:31

标签: android android-layout

我非常熟悉Android上的布局,但我无法解释这个:

<RelativeLayout
    android:id="@+id/mask_for_not_authenticated"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:gravity="center">

    <TextView
        android:id="@+id/notLoggedTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="You are not logged in, you haven't any profile to be shown" />

    <Button
        android:id="@+id/loginButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/notLoggedTV"
        android:padding="8dp"
        android:layout_centerHorizontal="true"
        android:text="What? How is it possible?! I want to be in!" />
</RelativeLayout>

this azure storage blog entry

当然,它在设备上看起来一样,并且浮动按钮不在布局中。而TextView正处于中间位置。

为什么结果不居中?我已经尝试layout_centerInParent并且也没有做任何事情(让gravity=center做),但没有成功。

5 个答案:

答案 0 :(得分:1)

试试这个:

<TextView
    android:id="@+id/notLoggedTV"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/loginButton"
    android:gravity="center"
    android:textColor="@color/black"
    android:text="You are not logged in, you haven't any profile to be shown" />

<Button
    android:id="@+id/loginButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:layout_centerInParent="true"
    android:text="What? How is it possible?! I want to be in!" />

android:gravity="center"删除RelativeLayout

答案 1 :(得分:1)

更改TextView宽度以匹配parrent并将重力设置为居中

   <RelativeLayout
    android:id="@+id/mask_for_not_authenticated"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:gravity="center_vertical"
    android:visibility="gone">

    <TextView
        android:id="@+id/notLoggedTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="You are not logged in, you haven't any profile to be shown" />

    <Button
        android:id="@+id/loginButton"
        android:layout_below="@id/notLoggedTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:layout_centerHorizontal="true"
        android:text="What? How is it possible?! I want to be in!" />
</RelativeLayout>

答案 2 :(得分:0)

您的代码中存在冗余。您的父母将重力设置为居中,因此您不需要在您的孩子中设置任何位置标记,以使其正确居中。

<RelativeLayout
android:id="@+id/mask_for_not_authenticated"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center">

<TextView
    android:id="@+id/notLoggedTV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="You are not logged in, you haven't any profile to be shown" />

<Button
    android:id="@+id/loginButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/notLoggedTV"
    android:padding="8dp"
    android:text="What? How is it possible?! I want to be in!" />

答案 3 :(得分:0)

这将为您提供所需的中心效果。我没有看到这是在RelativeLayout中。

<RelativeLayout
android:id="@+id/mask_for_not_authenticated"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">

<TextView
    android:id="@+id/notLoggedTV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="You are not logged in, you haven't any profile to be shown" />

<Button
    android:id="@+id/loginButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/notLoggedTV"
    android:padding="8dp"
    android:layout_below="@id/notLoggedTV"
    android:layout_centerHorizontal="true"
    android:text="What? How is it possible?! I want to be in!" />

答案 4 :(得分:0)

删除重力:

<RelativeLayout
    android:id="@+id/mask_for_not_authenticated"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_centerInParent="true"
        android:id="@+id/notLoggedTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="You are not logged in, you haven't any profile to be shown" />

    <Button

        android:id="@+id/loginButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/notLoggedTV"
        android:padding="8dp"
        android:layout_centerHorizontal="true"
        android:text="What? How is it possible?! I want to be in!" />
</RelativeLayout>
相关问题