2012-08-08 147 views
8

我剛開始創建android應用程序,但我有一個問題更改layout,每當我創建新的android項目,它給我RelativeLayout而不是LinearLayout,(好吧,基本上我是關注書教學,但它並沒有教我如何改變它)我假設必須有一個默認設置,以便我可以將Relativelayout更改爲LinearLayout更改Android佈局從相對線性

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

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:text="@string/hello_world" 
    tools:context=".MainActivity" /> 

</RelativeLayout> 

回答

8

要更改layout XML文件的Parent請按照下列步驟操作:

  • 右鍵點擊你的layout文件夾中的文件夾res從項目文件夾 月食。 (<project-folder/res/layout>
  • 選擇選項New -> Other...。推薦圖片:select layout folder
  • 從這裏選擇Android Layout XML file選項。 enter image description here
  • Next並選擇LinearLayout作爲Root Element並給出File Name。按Finish現在enter image description here
  • Layout XML有根元素爲LinearLayout
4

你只需要改變這個標籤<RelativeLayout ...><LinearLayout ...>,不要忘記用相同的佈局

0

當您創建在那個時候,你可以指定佈局,以新的XML文件,關閉它。但是當您創建新項目時,您無法更改佈局,因此在創建項目後必須對其進行更改。請轉到XML文件中的.xml選項卡,然後您可以編輯佈局。希望這可以幫助。

18

替換以下標籤:

<RelativeLayout 
    ... > 

</RelativeLayout> 

那些:

<LinearLayout 
    ... > 

</LinearLayout> 

此外,您應該刪除您的TextView以下屬性:

android:layout_centerHorizontal="true" 
android:layout_centerVertical="true" 

,因爲他們申請相對佈局而不是線性佈局。

+0

你應該加上'機器人:方向=「垂直」'如果你想用那種方式 - 它默認爲'horizo​​ntal' – mgarciaisaia 2016-03-17 15:24:20

7

如果您使用的是Eclipse,只是: 分辯點擊,改變佈局,並選擇您所選擇的佈局。

Img example:

enter image description here

+0

所有其他的答案也是正確的,這是事實,一個簡單的方法只是直接更改xml中的文本,但這正是我正在尋找的 - 如何做到「日食」的方式。謝謝。 – 2013-10-11 10:33:55

+1

@RainwalkerJake你知道在Android Studio中是否有類似的功能嗎?默認情況下,它總是相對佈局,我直到遲到時才意識到,並且討厭沒有簡單的方法來改變爲像Eclipse一樣的Linear。 – WORMSS 2015-03-04 11:12:27

1

機器人工作室的最新版本,我相信這將是做到這一點的方式。

進入水庫>佈局文件夾和右鍵點擊它,你會得到一個側邊欄,要求您要創建

轉到什麼類型的文件 - >編輯文件模板選項。

enter image description here

你會看到這個對話框打開。 你看這兩個選項右邊有內其他選項卡這些選項LayoutResourceFile.xmlLayoutResourceFile_vertical.xml

enter image description here

更改$ {} ROOT_TAG到中的LinearLayout這兩個的LayoutResourceFile.xmlLayoutResourceFile_vertical.xml並且您將始終獲得LinearLayout作爲未來的母公司。

Change