Android Studio Google Map V2 片段渲染

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23898992/
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 07:34:31  来源:igfitidea点击:

Android Studio Google Map V2 fragment rendering

androidgoogle-mapsandroid-layoutandroid-studio

提问by user3659544

I tried to write a test demo for Google Map V2 in Android Studio. I followed every step from Androidhive Google Map V2or better I think I need.

我尝试在 Android Studio 中为 Google Map V2 编写一个测试演示。我遵循了Androidhive Google Map V2或我认为我需要的更好的每一步。

I'm using the same layout_main.xml

我使用相同的 layout_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

but I'm getting always the same rendering problem:

但我总是遇到相同的渲染问题:

Rendering Problems
A <fragment>tag allows a layout file to dynamically include different layouts at runtime. At layout editing time the specific layout to be used is not known. You can choose which layout you would like previewed while editing the layout...

呈现问题
一个<fragment>标签允许布局文件,以动态地包括在运行时不同的布局。在布局编辑时,不知道要使用的特定布局。您可以在编辑布局时选择要预览的布局...

I have absolutely no idea what's the problem. Did I forget something? I just need to add Google services in the build.gradle like

我完全不知道有什么问题。我忘记了什么吗?我只需要在 build.gradle 中添加 Google 服务,例如

compile 'com.google.android.gms:play-services:4.4.52'

I copied the manifest from the demo and changed the API Key.

我从演示中复制了清单并更改了 API 密钥。

回答by John

The accepted answer is not wrong but it does not help.

接受的答案没有错,但无济于事。

As Dan wrote, Android Studio (also Version 1.0) will not display the map.
Maps needs an API key and dynamic processed code, maybe some day we will have that but so far not.
I've a 5 minute solution for those who want to see their app properly in layout preview: enter image description here

正如 Dan 所写,Android Studio(也是 1.0 版)不会显示地图。
地图需要一个 API 密钥和动态处理的代码,也许有一天我们会拥有,但到目前为止还没有。
对于那些希望在布局预览中正确查看应用程序的人,我有一个 5 分钟的解决方案: 在此处输入图片说明

To properly develop I still needed something else than a blank background.
I added overlays and buttons on top of the map, I really needed to see the map while placing elements over it.
The solution is simple:
1. Make a screenshot of your app with the map running (Power + Volume Down)
2. Download the screenshot and use an image editor to remove the top and bottom UI elements, so you will end up with only the map itself as an image.
3. Import that image into android studio drawables
4. create a new layout, name it dummy_mapviewfragment, put only a linearlayout and an imageview in
5. make the imageview "fill" the parent, and set "src" to the cropped image you just imported
6. back to your layout, add this into your Mapview Fragment xml :

为了正确开发,除了空白背景之外,我还需要其他东西。
我在地图顶部添加了叠加层和按钮,我真的需要在将元素放置在地图上的同时查看地图。
解决方案很简单:
1. 在地图运行的情况下制作您的应用的屏幕截图(电源 + 降低音量)
2. 下载屏幕截图并使用图像编辑器删除顶部和底部的 UI 元素,这样您最终只会得到将自身映射为图像。
3. 将该图像导入 android studio drawables
4. 创建一个新布局,命名它dummy_mapviewfragment,仅将一个线性布局和一个图像视图放入
5. 使图像视图“填充”父视图,并将“src”设置为您刚刚导入的裁剪图像
6.回到您的布局,将其添加到您的 Mapview Fragment xml 中:

 tools:layout="@layout/dummy_mapviewfragment"/>

That's it, now you will have a non-interactive mapview fragment which displays a real map.
Your app will look like it looks on your mobile phone. If you made errors in your image cropping you can "fix" it by setting the image scale to "centerCrop" so it will properly stretch out.

就是这样,现在您将拥有一个显示真实地图的非交互式地图视图片段。
您的应用程序看起来就像在您的手机上一样。如果您在图像裁剪中犯了错误,您可以通过将图像比例设置为“centerCrop”来“修复”它,使其正确伸展。

Update: You can get a screenshot without need to crop directly from within Androidstudio! Makes it a bit more convenient ;)

更新:您无需直接从 Androidstudio 中裁剪即可获取屏幕截图!让它更方便一点;)

回答by Dan Amato

Rendering Problems
A tag <fragment>allows a layout file to dynamically include different layouts at runtime. At layout editing time the specific layout to be used is not known. You can choose which layout you would like previewed while editing the layout...

渲染问题
标签<fragment>允许布局文件在运行时动态地包含不同的布局。在布局编辑时,不知道要使用的特定布局。您可以在编辑布局时选择要预览的布局...

This is just the preview window telling you that it can't display a preview for the <Fragment.../>tag, because it doesn't know what kind of fragment you're going to put in it. You can safely ignore that message - your actual app will render the fragment fine when you run it (as long as you code it up correctly!).

这只是一个预览窗口,告诉您它无法显示<Fragment.../>标签的预览,因为它不知道您要放入什么样的片段。您可以放心地忽略该消息 - 您的实际应用程序将在您运行时很好地呈现该片段(只要您正确编码!)。

回答by Himanshu Bhatnagar

Add a dummy layout "@android:layout/simple_gallery_item" as:

添加一个虚拟布局“@android:layout/simple_gallery_item”为:

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="317dp"
    android:layout_height="385dp"
    android:id="@+id/map"
    tools:context="com.example.XYZ.googlemaps.MapsActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    tools:layout="@android:layout/simple_gallery_item"/>

回答by Alberto Olmos

Android Studio automatically offers a solution within the error description, which is adding the name of the fragment layout.

Android Studio 会自动在错误描述中提供解决方案,即添加片段布局的名称。

tools:layout="@layout/fragment_my"

工具:layout="@layout/fragment_my"