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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11859073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:02:23  来源:igfitidea点击:

Change Android layout from Relative to Linear

androidandroid-layout

提问by George Lim

I just started creating android apps, but i have a problem changing the layout, every time when i create new android project , it gives me RelativeLayoutinstead of LinearLayout, (well, basically i am following the book instruction, but it doesn't teach me how to change it) I assume there must be a default setting so that i can change the Relativelayoutto LinearLayout.

我刚开始创建Android应用程序,但我已经改变了一个问题layout,每次当我创建新的Android项目时,它给了我RelativeLayout,而不是LinearLayout,(嗯,基本上我下面的书指示,但它并没有教我如何更改它)我假设必须有一个默认设置,以便我可以RelativelayoutLinearLayout.

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

采纳答案by sunil

To change the Parentof your layout XMLfile follow these steps:

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

  • Right click your layoutfolder in resfolder from project folder in eclipse. (<project-folder/res/layout>)
  • Choose option New -> Other.... Refer picture: select layout folder
  • Choose Android Layout XML fileoption from here. enter image description here
  • Press Nextand select LinearLayoutas Root Elementand give File Name. Press Finishenter image description here
  • Now your Layout XMLhas the root element as LinearLayout
  • 在 eclipse 中的项目文件夹中右键单击layout文件夹中的res文件夹。( <project-folder/res/layout>)
  • 选择选项New -> Other...。参考图片:选择布局文件夹
  • Android Layout XML file从这里选择选项。在此处输入图片说明
  • Next并选择LinearLayoutasRoot Element和 give File Name。按完成在此处输入图片说明
  • 现在你Layout XML的根元素为LinearLayout

回答by Leeeeeeelo

Replace the following tags :

替换以下标签:

<RelativeLayout
    ... >

</RelativeLayout>

by those :

由那些:

<LinearLayout
    ... >

</LinearLayout>

In addition, you should remove the following from your TextView attributes :

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

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

because they apply for the relative layout and not the linear layout.

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

回答by RainwalkerJake

If you are using Eclipse, just: Rigth click, change layout and select the layout of your choice.

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

Img example:

图像示例:

enter image description here

在此处输入图片说明

回答by AAnkit

You just need to change this tag <RelativeLayout ...>with <LinearLayout ...>, and don't forget to close it with same Layout

你只需要改变这个标签<RelativeLayout ...><LinearLayout ...>,不要忘记用相同的布局关闭

回答by TapanHP

In Latest versions of android studioi believe this will be the way to do it.

android studio 的最新版本中,我相信这将是做到这一点的方法。

Go to res-> layout folderand Right Click on it and you get a sidebar asking for what type of file you want to create

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

Go to -> Edit File Templatesoption.

转到 ->编辑文件模板选项。

enter image description here

在此处输入图片说明

You will see this dialog opened. You see both options right there inside Other tabThese options are LayoutResourceFile.xmland LayoutResourceFile_vertical.xml

您将看到此对话框已打开。您可以在其他选项卡中看到两个选项这些选项是LayoutResourceFile.xmlLayoutResourceFile_vertical.xml

enter image description here

在此处输入图片说明

Change the ${ROOT_TAG}to LinearLayout in both of these LayoutResourceFile.xmland LayoutResourceFile_vertical.xmland you will always get LinearLayoutas parent for future.

将这两个LayoutResourceFile.xmlLayoutResourceFile_vertical.xml 中${ROOT_TAG}更改为 LinearLayout ,您将始终获得LinearLayout作为未来的父级。

Change

改变

回答by Akshay

When you create new XML file at that time you can assign layout to it.But when you create new project at that time you can not change layout you have to change it after creation of project.So go to .xml tab in XML file there you can edit layout. Hope this helps.

当您创建新的 XML 文件时,您可以为其分配布局。但是当您创建新项目时,您无法更改布局,您必须在创建项目后更改它。所以转到 XML 文件中的 .xml 选项卡您可以编辑布局。希望这可以帮助。