2012-10-29 35 views
0

我有下一個問題:我使用了很多RelativeLayout,就像Android文檔告訴我要做的那樣。但是,因爲過幾天,我不明白爲什麼,這是不工作了:RelativeLayout無法正常工作Android

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:background="@drawable/background" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <Button 
     android:id="@+id/boton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="Button" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/boton" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="60dp" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="#000" /> 

</RelativeLayout> 

這是我的佈局,非常簡單。所以,根據這個,textView應該在按鈕之下,但是,相反,它會出現在按鈕上!

This is a picture of the result

誰有解釋我會發生什麼?

編輯:這個問題是不是當我嘗試在設備上運行它,問題就來了,當我看到它在ADT Eclipse插件的圖形佈局...

+0

難道你結束'RelativeLayout',你的代碼是爲我工作。 '' – jnthnjns

+0

你的代碼對我來說也很好。 你是否結束了相對佈局標籤Asok說 –

+0

工作正常關閉相對佈局標籤。 – Raghunandan

回答

0

我覺得你的問題是drawable作爲背景,因爲我在預覽中看不到圖像。

有此問題給你一個build error,所以預覽不會顯示其當前狀態的佈局(它恢復到最後的成功構建)。要麼刪除該行並保存到查看預覽或檢查,以確保您在標題爲「背景」你drawable小號文件夾中的圖像

試試這個,不被拉伸,以檢查它是否是爲你工作:

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

    <Button 
     android:id="@+id/boton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="Button" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/boton" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="60dp" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="#000" /> 

</RelativeLayout> 
-1

在您的相對佈局的末尾添加</RelativeLayout>以關閉它。

+1

-1用於閱讀評論並將其添加爲答案。 – jnthnjns

0

從TextView的layout_below屬性中取出「+」符號。 '+'告訴它定義一個新的ID。沒有'+'就應該參考現有的。

+0

它可以使用或不使用'+',儘管'+'不應該在那裏。 – jnthnjns

1

我試試這個代碼它正常工作。

如果你身邊的工作不正常嘗試清理建立你的項目,然後運行。

如果仍然有問題,那麼嘗試此

android:layout_below="@+id/boton" 

除去上面的行與此

android:layout_below="@id/boton" 

因爲 「+」 創建新的參考文件R.java

這可能是問題。

0

我試圖運行它,現在,我知道是什麼問題。

在終端中運行應用程序時,它運行正常,當我嘗試在Eclipse上的ADT插件上的圖形佈局上看到問題時,出現問題!任何人都有同樣的問題?

1

嘗試通過從代碼 的Android刪除以下行:layout_marginTop =「60dp」

相關問題