2015-11-05 77 views
1

我非常熟悉Android上的佈局,但我無法解釋這一個:奇怪布點的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> 

enter image description here

當然,它看起來在設備上的相同和浮動按鈕沒有在佈局中。而TextView在中間。

爲什麼結果不居中?我嘗試過layout_centerInParent,也沒有任何東西(讓gravity=center做),沒有成功。

+0

唯一的異常,我可以看到的是額外的** ** +'這裏的android:layout_below = 「@ + ID/notLoggedTV」'。但是,也許這個'android:layout_centerHorizo​​ntal =「true」'會干擾父母的'android:gravity =「中心'' –

+0

這就是我在運行時不會遇到問題的原因,因爲您引用的視圖沒有創建,非常有用。 。但不是,不改變任何東西 –

+0

'你所指的視圖不是被創建的'錯誤的:我可以看到它**已經被創建(就在按鈕之上)。如果TextView在Button下面聲明**,那麼是的,你會是對的。 –

回答

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

等待'TextView'好吧.... –

+0

嘗試在你的TextView中加入這個android:layout_centerVertical =「true」 android:layout_centerHorizo​​ntal =「true」'和你的按鈕'android:layout_centerHorizo​​ntal =「true」' – Yovhi

+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!" /> 

RelativeLayout取出android:gravity="center"

+0

這更好,但是你有解釋爲什麼'TextView'消失...? o_O –

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!" /> 

+0

我討厭引用自己:'也放了什麼(讓重力=中心做)'已經嘗試過你的解決方案,但不錯的嘗試! –

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!" /> 
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>