2012-04-21 85 views
1

我不明白爲什麼下面的代碼將我的TextView放置在屏幕的左上角。根據我對layout_gravity的理解,它似乎應該將我的TextView放在屏幕的底部。Android layout_gravity

任何人都可以請擺脫這一點對我來說?這種線性佈局儘管看起來非常簡單,卻令我頭痛不已。看起來如此簡單的事情怎麼能成爲如此皇家般的痛苦?

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" 
     android:background="#FF0000"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/hello" 
      android:layout_gravity="bottom" 
      android:background="#FFF" 
      />  

    </LinearLayout> 

回答

0
<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#FF0000"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/hello" 
      android:layout_alignParentBottom="true" 
      android:background="#FFF" 
      />  

    </RelativeLayout> 

可以使用相對佈局中,您可以對齊底部容易

+0

感謝您的提示。我傾向於使用RelativeLayout來處理大多數「實際」佈局。我之所以使用這個例子只是因爲我想在移動到RelativeLayout之前完全理解LinearLayout。我剛剛開始意識到,起初看起來如此簡單的事情,根據我對文檔解釋的理解,最終變得完全不直觀。我想不出一個比這更簡單的例子,但是當它按照我想要的方式定位文本時,它給我帶來了很多問題。 – 2012-04-21 17:31:18

+0

如果只是想要水平或垂直佈局,那麼使用線性的else會更好地使用相對。 ofcourse re; ative雖然然後線性,但一旦你理解它,它是更容易然後線性 – 2012-04-22 02:14:14

0

我覺得這個代碼做你想要什麼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:gravity="bottom" 
    android:background="#FF0000"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" 
     android:background="#FFF" 
     />  

</LinearLayout> 

如果你想在中心TextView的,你可以使用:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:gravity="bottom" 
    android:background="#FF0000"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" 
     android:layout_gravity="center" 
     android:background="#FFF" 
     />  

</LinearLayout> 

希望這對你有所幫助。

+0

什麼hapeens,如果他有超過一個子視圖的線性佈局,那麼將在底部對齊或不是? – 2012-04-21 16:35:21

+0

@Agarwal,是的,但我建議你嘗試一下,以更好地瞭解發生了什麼...... – amp 2012-04-21 16:41:35

0

如果這是您佈局的簡單性,那麼您不應該將其包裝在LinearLayout中。

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom" 
    android:background="#FFF" 
    android:text="HELLO HELLO" /> 

如果需要,雖然它包,取出垂直方向:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FF0000" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:background="#FFF" 
     android:text="HELLO HELLO" /> 

</LinearLayout> 
+0

爲什麼刪除垂直方向會導致layout_gravity以我期望的方式工作?垂直方向是否會改變layout_gravity的功能?它看起來確實如此。不過,我無法理解這種行爲背後的邏輯。我相信有一個很好的理由,我現在只是沒有看到它。 – 2012-04-21 17:35:48

+0

Ooops ...這是在這篇文章下面的文章中解釋的。當我發佈我的第一條評論時,還沒有完全實現。:) – 2012-04-21 17:36:42

+0

@JustinJenkins由於垂直線性佈局忽略了底部重力,因爲它試圖堆疊兒童'垂直'任何水平重力將通過 – Blundell 2012-04-21 22:57:04

2

一個的LinearLayout設置爲垂直方向將忽略是指垂直對齊所有layout_gravity參數。因此,例如「底部」不會工作,但「中心水平」將工作。 (垂直對齊始終完成,以便佈局中的每個視圖放置在佈局中的其他視圖下。)

相對佈局可能是要走的路或者也許您可以使用此代碼,可能非常接近到你想要的。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#FF0000" 
> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" 
     android:gravity="bottom" 
     android:layout_weight="1.0" 
     android:background="#FFF" /> 

</LinearLayout> 

編輯blog進一步解釋的事情。

+0

非常感謝這個解釋!我現在知道了。非常感激! – 2012-04-21 17:37:36

+0

@Justin Jenkins如果它有幫助,那麼你必須將其標記爲已解決,通過這種方式,人們來到你的帖子將會知道這是最適合你的問題的答案... – 2012-04-21 19:02:56

+0

但是,但他只是偷走了我的答案: - ( – Blundell 2012-04-21 22:56:03

相關問題