将Android布局从“相对”更改为“线性”

时间:2012-08-08 06:47:24

标签: android android-layout

我刚刚开始创建Android应用程序,但我在更改layout时遇到问题,每当我创建新的Android项目时,它都会给我RelativeLayout而不是LinearLayout,(好吧) ,基本上我按照书中的说明进行操作,但它并没有教我如何更改它)我假设必须有一个默认设置,以便我可以将Relativelayout更改为LinearLayout。 / p>

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

6 个答案:

答案 0 :(得分:19)

替换以下标记:

<RelativeLayout
    ... >

</RelativeLayout>

由那些人:

<LinearLayout
    ... >

</LinearLayout>

此外,您应该从TextView属性中删除以下内容:

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

因为它们适用于相对布局而不是线性布局。

答案 1 :(得分:7)

要更改Parent文件的layout XML,请按以下步骤操作:

  • 右键单击项目文件夹中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。按完成enter image description here
  • 现在,您的Layout XML的根元素为LinearLayout

答案 2 :(得分:7)

如果您使用的是Eclipse,只需: 右键单击,更改布局并选择所选的布局。

Img example:

enter image description here

答案 3 :(得分:4)

您只需要使用<RelativeLayout ...>更改此标记<LinearLayout ...>,并且不要忘记使用相同的布局关闭它

答案 4 :(得分:1)

android studio 的最新版本中,我相信这将是实现它的方法。

转到res-&gt;布局文件夹并右键单击它,您会看到一个侧边栏,询问您要创建的文件类型

转到 - &gt; 修改文件模板选项。

enter image description here

您将看到此对话框已打开。 您可以在其他标签内看到这两个选项。这些选项是 LayoutResourceFile.xml LayoutResourceFile_vertical.xml

enter image description here

$ {ROOT_TAG} 更改为 LayoutResourceFile.xml LayoutResourceFile_vertical.xml 中的LinearLayout,您将始终获得 LinearLayout 作为未来的父母。

Change

答案 5 :(得分:0)

当你创建新的XML文件时,你可以为它分配布局。但是当你创建新项目时,你不能改变布局,你必须在创建项目后更改它。所以转到.xml选项卡那里的XML文件可以编辑布局。希望这会有所帮助。