我正在尝试创建一个使用Google Maps的android应用,并尝试将其添加到所有活动应用的导航抽屉中,但该地图未显示。我正在尝试开发一个使用抽屉在活动之间导航的应用,其中一项是使用Google Maps创建的。地图活动开始,出现带有“地图准备就绪”的祝酒消息,当我单击灰色屏幕(应该在其中显示地图)时,我收到祝酒消息,告诉我经度和纬度,这意味着地图是在布局背面以某种方式工作。我相信这种情况是由于某些布局文本写得不正确造成的,但是我无法找到原因,因此我将在此处添加xml的文件,也许有人会发现我没有看到的错误。我还尝试将包含地图的片段放在xml的最后,因为我读到这可能是没有显示地图的原因,但仍然没有变化。在MapsActivity.java中,我还有一个linearlayout.setVisibility(View.GONE)
,如果我更改为VISIBLE
,它会在不存在的地图上显示在屏幕上。谢谢!
activity_maps.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<fragment xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="com.example.navigationdrawerexemplu.MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:elevation="10dp"
android:background="@drawable/white_border"
android:id="@+id/relLayout1">
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:id="@+id/search"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:src="@drawable/ic_search"/>
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/search"
android:layout_centerVertical="true"
android:textSize="15sp"
android:textColor="#000"
android:id="@+id/cautare"
android:background="@null"
android:hint=" Cauta o adresa"
android:imeOptions="actionSearch"/>
</RelativeLayout>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/relLayout1"
android:layout_marginTop="50dp"
android:background="@drawable/white_border"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:id="@+id/trotuar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:src="@drawable/ic_trotuar" />
<Button
android:id="@+id/buton_trotuar"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Trotuar neamenajat corespunzator "
android:background="@drawable/button_selectro"
android:onClick="onClick"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:id="@+id/lucrari"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:src="@drawable/ic_lucrari" />
<Button
android:id="@+id/buton_lucrari"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Lucrari nesemnalizate"
android:background="@drawable/button_selectro"
android:onClick="onClick" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:id="@+id/semafor"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:src="@drawable/ic_semafor" />
<Button
android:id="@+id/buton_semafor"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Semafor nefunctional"
android:background="@drawable/button_selectro"
android:onClick="onClick" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:id="@+id/parcare"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:src="@drawable/ic_semnparcare" />
<Button
android:id="@+id/buton_parcare"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Parcare neregulamentare"
android:background="@drawable/button_selectro"
android:onClick="onClick" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:id="@+id/iluminat"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:src="@drawable/ic_iluminat" />
<Button
android:id="@+id/buton_iluminat"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Iluminat stradal defect"
android:background="@drawable/button_selectro"
android:onClick="onClick" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:id="@+id/igiena"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:src="@drawable/ic_igiena" />
<Button
android:id="@+id/buton_igiena"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Igiena publica"
android:background="@drawable/button_selectro"
android:onClick="onClick" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:id="@+id/transport"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:src="@drawable/ic_bus" />
<Button
android:id="@+id/buton_transport"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Situatii legate de carosabil"
android:background="@drawable/button_selectro"
android:onClick="onClick" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:id="@+id/altele"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:src="@drawable/ic_setari" />
<Button
android:id="@+id/buton_altele"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Altele"
android:background="@drawable/button_selectro"
android:onClick="onClick" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:id="@+id/INAPOI"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:src="@drawable/ic_inapoi" />
<Button
android:id="@+id/buton_inapoi"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Inapoi"
app:backgroundTint="@color/white" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/linearLayout"
android:layout_centerHorizontal="true"
android:id="@+id/layout_buton"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Raporteaza"
android:textSize="20dp"
android:backgroundTint="@android:color/holo_red_dark"
android:gravity="center"
android:id="@+id/raporteaza"
android:onClick="Raporteaza"/>
</RelativeLayout>
<LinearLayout
android:id="@+id/layout_buton_acasa"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Acasa"
android:id="@+id/buton_home"/>
</LinearLayout>
</RelativeLayout>
MapsActivity(仅关于布局的代码,其余关于获得许可,添加标记等):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.activity_maps, frameLayout);
frameLayout
从BaseActivity中的frameLayout = (FrameLayout) findViewById(R.id.content_frame);
中使用。content_frame
来自navigation_drawer.xml
,在BaseActivity中用作setContentView
。navigation_drawer.xml:
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</androidx.drawerlayout.widget.DrawerLayout>
在Activity
的情况下,您必须使用setContentView
而不是Inflater
。尝试如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
....
}