2012-08-08 146 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