Android 使用片段活动创建的 ADT 空白活动..
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22289164/
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
ADT blank activity created with fragment activity..
提问by Christian Burgos
i am really confused right now because whenever i create a new android app with blank activity it always comes out with fragment_main.xml.. i just wanted to create a blank activity without the fragment one...
我现在真的很困惑,因为每当我创建一个带有空白活动的新 android 应用程序时,它总是会出现 fragment_main.xml ..我只想创建一个没有片段的空白活动......
in the first image the blank activity comes with the fragment layout..
在第一张图片中,空白活动带有片段布局..
the second image shows the created fragment_main
第二张图显示了创建的 fragment_main
now i am really confused... this only happened after updating ADT to the latest version i have referred to this thread: https://stackoverflow.com/questions/22203862/adt-doesnt-create-default-hello-world-but-command-line-does#=
现在我真的很困惑......这只是在将 ADT 更新到我提到的这个线程的最新版本之后才发生的:https: //stackoverflow.com/questions/22203862/adt-doesnt-create-default-hello-world-but -command-line-does# =
i just wanted to make an android app with blank activity with no fragment view...
我只是想制作一个没有片段视图的空白活动的Android应用程序......
回答by Ben Jakuben
For those who would like instructions on how to remove Fragments from the project:
对于那些想要关于如何从项目中删除 Fragments 的说明的人:
1) Copy all the contents of res/layout/fragment_main.xml. Open activity_main.xml, delete the FrameLayout, and paste in the copied contents.
1)复制res/layout/fragment_main.xml的所有内容。打开activity_main.xml,删除FrameLayout,粘贴复制的内容。
2) Delete fragment_main.xml
2)删除fragment_main.xml
3) In MainActivity.java, delete the whole PlaceHolderFragment class:
3) 在 MainActivity.java 中,删除整个 PlaceHolderFragment 类:
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main,
container, false);
return rootView;
}
}
4) Delete the following lines from onCreate():
4) 从 onCreate() 中删除以下几行:
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
At this point you should be all set to run the project.
此时,您应该已准备好运行该项目。
回答by saran
You can use the previous template by editing the template files inside the Android SDK folder. But, make sure you have a backup of the "BlankActivity" folder.
您可以通过编辑 Android SDK 文件夹中的模板文件来使用以前的模板。但是,请确保您有“BlankActivity”文件夹的备份。
Go to /templates/tools/templates/activities/BlankActivity
Locate the template.xmlfile and locate the following piece of code
<parameter id="fragmentLayoutName" name="Fragment Layout Name" type="string" constraints="layout|unique|nonempty" suggest="fragment_${classToResource(activityClass)}" default="fragment_main" help="The name of the layout to create for the activity's content fragment" />
转到/templates/tools/templates/activities/BlankActivity
找到template.xml文件,找到如下一段代码
<parameter id="fragmentLayoutName" name="Fragment Layout Name" type="string" constraints="layout|unique|nonempty" suggest="fragment_${classToResource(activityClass)}" default="fragment_main" help="The name of the layout to create for the activity's content fragment" />
and change the constraints
to constraints="empty"
.
并将 更改constraints
为constraints="empty"
。
Locate recipie.xml.ftland locate the following piece of code
<instantiate from="res/layout/fragment_simple.xml.ftl" to="${escapeXmlAttribute(resOut)}/layout/${fragmentLayoutName}.xml" />
and comment the whole line.Locate SampleActivity.java.ftlfile inside root/src/app_packageand delete these two lines
<#include "include_options_menu.java.ftl"> <#include "include_fragment.java.ftl">
Locate fragment_simple.xml.ftland activity_fragment_container.xml.ftlfile inside root/res/layout. Copy the contents of fragment_simple.xml.ftl fileto activity_fragment_container.xml.ftlfile.
找到recipie.xml.ftl并找到以下代码段
<instantiate from="res/layout/fragment_simple.xml.ftl" to="${escapeXmlAttribute(resOut)}/layout/${fragmentLayoutName}.xml" />
并注释整行。在root/src/app_package 中找到SampleActivity.java.ftl文件并删除这两行
<#include "include_options_menu.java.ftl"> <#include "include_fragment.java.ftl">
在root/res/layout 中找到fragment_simple.xml.ftl和activity_fragment_container.xml.ftl文件。将fragment_simple.xml.ftl 文件的内容复制到activity_fragment_container.xml.ftl文件。
Now when you try to create a new activity, you'll get this screen
现在,当您尝试创建新活动时,您将看到此屏幕
You can leave the Fragment Layout Namefield blank.
您可以将片段布局名称字段留空。
This works fine for me in the case of blank activity. I'm not sure if this is the right approach, with discarding fragments and all, but this works for me.
在空白活动的情况下,这对我来说很好用。我不确定这是否是正确的方法,丢弃片段等等,但这对我有用。
回答by Muzikant
This is a new feature of ADT version 22.6.0: http://developer.android.com/tools/sdk/eclipse-adt.html
这是 ADT 22.6.0 版的新功能:http: //developer.android.com/tools/sdk/eclipse-adt.html
Edit: With the latest ADT updates there is a new template called "Empty Activity" that has no fragments. it's a plain class that extends Activity
(Even without the default menu).
编辑:随着最新的 ADT 更新,有一个名为“Empty Activity”的新模板,它没有片段。它是一个扩展的普通类Activity
(即使没有默认菜单)。
Notice that there is also a "Blank Activity" which extends ActionBarActivity
and has fragments
请注意,还有一个“空白活动”,它扩展ActionBarActivity
并具有片段
回答by Chris
Thank you to the person that reported this, https://code.google.com/p/android/issues/detail?id=67513
感谢报告此问题的人,https://code.google.com/p/android/issues/detail?id=67513
This was my first glimpse into android-review.googlesource.com. Digging around I didn't see a way to download the patch set that was put together and I'm unsure how to “cherry pick” tools_r22.6, If there's a thread that explains it a link would be appreciated.
这是我第一次看到 android-review.googlesource.com。四处挖掘,我没有看到一种下载组合补丁集的方法,我不确定如何“挑选”tools_r22.6,如果有一个解释它的线程,将不胜感激。
Here's how I went about it.
这就是我如何去做的。
On your machine navigate to your SDK folder then \tools\templates\activities
在您的机器上导航到您的 SDK 文件夹然后 \tools\templates\activities
Copy the BlankActivities folder and paste it into the same directory.
复制 BlankActivity 文件夹并将其粘贴到同一目录中。
Rename BlankActivities_copy to EmptyActivities
将 BlankActivity_copy 重命名为 EmptyActivity
Go to https://android-review.googlesource.com/#/c/88890/4
转到https://android-review.googlesource.com/#/c/88890/4
Under the Files section there is a list of links. Each link directs you to the xml that needs to be added to the new EmptyActivities folder you created.
在文件部分下有一个链接列表。每个链接都将您定向到需要添加到您创建的新 EmptyActivities 文件夹中的 xml。
Copy the xml from the right pane to the path\file at the top of the left pane(the link was also the path). If the file already exists delete the contents and replace. If the file doesn't exist, create it and copy the xml into the file.
将右侧窗格中的 xml 复制到左侧窗格顶部的 path\file(链接也是路径)。如果文件已经存在,删除内容并替换。如果文件不存在,请创建它并将 xml 复制到文件中。
Repeat for each of the links.
对每个链接重复。
Close and reopen Eclipse. If you have any of the files open in an editor Eclipse will crash.
关闭并重新打开 Eclipse。如果您在编辑器中打开了任何文件,Eclipse 将崩溃。
Thank you to Josiah Gaskin at google with the unbelievable 2 day turnaround!
感谢 google 的 Josiah Gaskin 令人难以置信的 2 天周转!
No training literature references the fragments in the BlankActivity. I'm sure this fix saved me countless hours trying to figure out how to navigate around fragments while trying to learn a new IDE. Thanks again.
没有培训文献引用 BlankActivity 中的片段。我确信这个修复为我在尝试学习新 IDE 的同时试图弄清楚如何在片段中导航时节省了无数时间。再次感谢。
回答by EyalBellisha
If you download the latest ADT 22.6.3
(with build tools 19.0.3) you will have the option to create a new Empty Activity
that doesn't use fragments
如果您下载最新的ADT 22.6.3
(使用构建工具 19.0.3),您将可以选择创建一个Activity
不使用片段的新 Empty
回答by Walker Rowe
We found a solution to this:
我们找到了解决方案:
When you create a project select "empty" activity. Then it create a MainActivity class that extends Activity and an activity_main.xml layout.
创建项目时,选择“空”活动。然后它创建一个扩展 Activity 的 MainActivity 类和一个 activity_main.xml 布局。
In other words, it give you the behavior that was there before.
换句话说,它为您提供以前存在的行为。
This worked in ADT 22.6.3.
这在 ADT 22.6.3 中有效。
回答by Sravani
I have also faced the same issue. I just deleted the eclipse and again downloaded the ADT bundle from http://developer.android.com/sdk/index.html#downloadTo recover your previous projects, just change the work-space to your previous one. This worked for me.
我也遇到过同样的问题。我刚刚删除了 eclipse 并再次从http://developer.android.com/sdk/index.html#download下载了 ADT 包 要恢复您以前的项目,只需将工作空间更改为您以前的项目。这对我有用。
回答by Shubham Naik
If you don't want the fragment part to be in your app then simply "unckeck the create activityoption at project setup wizard, then manually create the activity and layout" for your project.
如果您不希望片段部分出现在您的应用程序中,那么只需“在项目设置向导中取消选中创建活动选项,然后 为您的项目手动创建活动和布局”。
回答by user3459475
A solution to this, is a copy of the older "Blank Activity" in %Wherever your ADT bundle is%\sdk\tools\templates\activities\
.
对此的解决方案是在%Wherever your ADT bundle is%\sdk\tools\templates\activities\
.
If someone has an older version of it, you can rename it, place it in here %Wherever your ADT bundle is%\sdk\tools\templates\activities\
and then when starting your project, select this folder instead of "Blank Activity", so if you have a copy please share.
如果有人有它的旧版本,你可以重命名它,把它放在这里%Wherever your ADT bundle is%\sdk\tools\templates\activities\
,然后在启动你的项目时,选择这个文件夹而不是“空白活动”,所以如果你有一个副本,请分享。
回答by Rashid
While creating a new Application, just copy the Layout Name to the Fragment Layout Name (e.g. activity_main)
创建新应用程序时,只需将布局名称复制到片段布局名称(例如activity_main)
Voila!!! You get an activity without the fragment part.
瞧!!!你会得到一个没有片段部分的活动。
Remove this portion of the code from activity:
从活动中删除这部分代码:
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
Clean the project.
清理项目。