将 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
Change Android layout from Relative to Linear
提问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 RelativeLayout
instead 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 Relativelayout
to LinearLayout
.
我刚开始创建Android应用程序,但我已经改变了一个问题layout
,每次当我创建新的Android项目时,它给了我RelativeLayout
,而不是LinearLayout
,(嗯,基本上我下面的书指示,但它并没有教我如何更改它)我假设必须有一个默认设置,以便我可以Relativelayout
将LinearLayout
.
<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 Parent
of your layout XML
file follow these steps:
要更改Parent
您的layout XML
文件,请按照下列步骤操作:
- Right click your
layout
folder inres
folder from project folder in eclipse. (<project-folder/res/layout>
) - Choose option
New -> Other...
. Refer picture: - Choose
Android Layout XML file
option from here. - Press
Next
and selectLinearLayout
asRoot Element
and giveFile Name
. Press Finish - Now your
Layout XML
has the root element asLinearLayout
- 在 eclipse 中的项目文件夹中右键单击
layout
文件夹中的res
文件夹。(<project-folder/res/layout>
) - 选择选项
New -> Other...
。参考图片: Android Layout XML file
从这里选择选项。- 按
Next
并选择LinearLayout
asRoot Element
和 giveFile 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,只需:右键单击,更改布局并选择您选择的布局。
回答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.
转到 ->编辑文件模板选项。
You will see this dialog opened. You see both options right there inside Other tabThese options are LayoutResourceFile.xmland LayoutResourceFile_vertical.xml
您将看到此对话框已打开。您可以在其他选项卡中看到两个选项这些选项是LayoutResourceFile.xml和LayoutResourceFile_vertical.xml
Change the ${ROOT_TAG}to LinearLayout in both of these LayoutResourceFile.xmland LayoutResourceFile_vertical.xmland you will always get LinearLayoutas parent for future.
将这两个LayoutResourceFile.xml和LayoutResourceFile_vertical.xml 中的${ROOT_TAG}更改为 LinearLayout ,您将始终获得LinearLayout作为未来的父级。
回答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 选项卡您可以编辑布局。希望这可以帮助。