简体   繁体   English

无法在 Android Studio 中向 activity_main.xml 添加任何项目

[英]Can't add any items to activity_main.xml in Android Studio

In Android Studio, I can't add any items from Palette to activity_main.xml in Design view.在 Android Studio 中,我无法将 Palette 中的任何项目添加到设计视图中的 activity_main.xml。 It just won't let me drag and drop them.它只是不会让我拖放它们。 Any idea why this happens?知道为什么会这样吗?

Here is the PrintScreen:这是打印屏幕:

According to new design method followed in android studio for android development you cannot add any elements to the activity_main.xml.根据android studio 中为android 开发遵循的新设计方法,您不能向activity_main.xml 添加任何元素。 Rather you should add them to content_main.xml.相反,您应该将它们添加到 content_main.xml。

You can learn more about it from this answer.您可以从这个答案中了解更多信息。

You can stop it from happening.你可以阻止它发生。
Answered by BNK : BNK 的回答:

If you create a new project, you can choose Empty Activity template instead of Blank Activity.如果您创建一个新项目,您可以选择 Empty Activity 模板而不是 Blank Activity。 If you create a new activity, you can choose Empty Activity instead of Blank Activity too.如果您创建一个新活动,您也可以选择 Empty Activity 而不是 Blank Activity。

how do I create an Empty Activity with a Fragment?如何使用片段创建空活动?

You start by creating an activity using the "Empty Activity" template.首先使用“空活动”模板创建活动。 Then, add a fragment class yourself or possibly via New > Fragment.然后,自己添加一个片段类,或者可能通过 New > Fragment 添加一个片段类。 Please understand that all of the "New >" options, for activities, fragments, etc., are all driven by templates.请理解,活动、片段等的所有“新建>”选项都是由模板驱动的。 Those templates do what they do.这些模板做他们所做的。 If you do not want what they are generating, don't use them, and add the classes, resources, manifest entries, and such yourself.如果您不想要它们生成的内容,请不要使用它们,并自行添加类、资源、清单条目等。 Personally, I almost never use those templates, because it's simpler IMHO just to create it all yourself than to rip out all the stuff you don't need that gets generated.就个人而言,我几乎从不使用这些模板,因为恕我直言,自己创建它比撕掉生成的所有不需要的东西要简单。

I had same problem.我有同样的问题。 In the design page, I saw error message "Render Error..." I click Android Studio->check for update->update and restart After android studio update, the drag&drop from Palette to design page works.在设计页面中,我看到错误消息“渲染错误...”我点击 Android Studio-> 检查更新-> 更新并重新启动 android studio 更新后,从 Palette 拖放到设计页面工作。

Check ConstraintLayout properties:
layout width and layout height should be "match parent", not "wrap content". 

It works for me.这个对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM