簡體   English   中英

Android 2.x中的infowindow背景發生錯誤

[英]Error occurs with infowindow background in Android 2.x

這是我在所有信息窗口中都過大的問題,我想設置為180dp高度和300dp寬度,或者設置為warp_content:

我意識到問題僅出現在2.2和Android 2.3版本中,在Android 4.1上沒有問題,並且我不嘗試在3.x版本上使用

知道如何解決嗎?

我讀了另getInfoContent文章,說改變了getInfoContentgetInfoWindow ,但是我已經有了。

在此處輸入圖片說明

InfoWindow.xml:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#243439"
android:orientation="horizontal" >

<ImageView 
android:id="@+id/imagen"
android:layout_width="114dp"
android:layout_weight="0.5"
android:layout_height="174dp"
android:contentDescription="@string/app_name"/>

<LinearLayout
android:layout_width="174dp"
android:layout_height="174dp"
android:orientation="vertical" >

<TextView
android:id="@+id/titulo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textColor="#FFFFFF" 
android:gravity="center_horizontal|center"
android:textSize="17sp"
android:textStyle="bold" />

<TextView
android:id="@+id/direccion"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:textColor="#FFFFFF" 
android:gravity="center_horizontal|center"
android:textSize="12sp" />

<TextView
android:id="@+id/vermas"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:textColor="#FFFFFF" 
android:gravity="bottom|center_horizontal"
android:text="@string/vermas"
android:textSize="12sp" />

</LinearLayout>
</LinearLayout>

fragment.java:

googlemap.setInfoWindowAdapter(new InfoWindowAdapter() {

  @Override
  public View getInfoWindow(Marker marker) {

  View v = getLayoutInflater(null).inflate(R.layout.infowindow, null);

  TextView titulo = (TextView) v.findViewById(R.id.titulo);
  TextView direccion = (TextView) v.findViewById(R.id.direccion);
  ImageView imagen = ((ImageView)v.findViewById(R.id.imagen));

  titulo.setText(marker.getTitle());
  direccion.setText(marker.getSnippet());

  if(hash.get(marker) != null)
  imagen.setImageDrawable(getResources().getDrawable(hash.get(marker)));

  return v;

  }
  //..
  @Override
  public View getInfoContents(Marker marker) {
 // TODO Auto-generated method stub
return null;
  }
  });
  }

看,我發布了我擁有的最終代碼,希望對您有所幫助

Infowindow.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="294dp"
android:layout_height="180dp"
android:background="@drawable/rectangle_infowindow"
android:gravity="center"
android:orientation="horizontal" >

<ImageView 
    android:id="@+id/imagen"
    android:layout_width="114dp"
    android:layout_height="174dp"
    android:contentDescription="@string/app_name"/>

<LinearLayout
    android:layout_width="174dp"
    android:layout_height="174dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/titulo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:textColor="#FFFFFF" 
        android:gravity="center_horizontal|center"
        android:textSize="17sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/direccion"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="14dp"
        android:textColor="#FFFFFF" 
        android:gravity="center_horizontal|center"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/vermas"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:textColor="#FFFFFF" 
        android:gravity="bottom|center_horizontal"
        android:text="@string/vermas"
        android:textSize="12sp" />

    </LinearLayout>
</LinearLayout>

片段

import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;


public class Fragment3 extends SherlockFragment {
    //create hashmap
            private HashMap<Marker, Integer> hash = new HashMap<Marker, Integer>();
            private HashMap<Marker, Class<?>> hashclass = new HashMap<Marker, Class<?>>();


            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment3, null);
                return rootView;
            }

    @Override
    public void onViewCreated(View v, Bundle savedInstanceState){
        super.onViewCreated(v, savedInstanceState);

    final LatLng Initial = new LatLng(-34.673009, -58.474111);
    final LatLng FADU = new LatLng(-34.542163, -58.443716);
    final LatLng UNO = new LatLng(-34.533721, -58.508304);
    final LatLng DOSa = new LatLng(-34.664732, -58.360914);
    final LatLng DOSb = new LatLng(-34.620405, -58.371846);


GoogleMap googlemap;

        googlemap  = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map3)).getMap();

        googlemap.setMyLocationEnabled(true);
        googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(Initial, 10);
        googlemap.animateCamera(update);


      //modify
            Marker marker1 = googlemap.addMarker(new MarkerOptions().position(FADU).title("FADU").snippet("Facultad de Arquitectura, Diseño y Urbanismo").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker1)));
            hash.put(marker1, R.drawable.logo);
            hashclass.put(marker1, Contacto.class);


            googlemap.setOnInfoWindowClickListener(new OnInfoWindowClickListener(){

                @Override
                public void onInfoWindowClick(Marker marker) {
                    // TODO Auto-generated method stub
                    Class<?> cls = hashclass.get(marker);
                    Intent i = new Intent(getActivity(), cls);
                    startActivity(i);
                }
            });

            googlemap.setInfoWindowAdapter(new InfoWindowAdapter() {

                 @Override
                 public View getInfoWindow(Marker marker) {

                 View v =     getLayoutInflater(null).inflate(R.layout.infowindow, null);

             TextView titulo = (TextView) v.findViewById(R.id.titulo);
             TextView direccion = (TextView) v.findViewById(R.id.direccion);
                 ImageView imagen = ((ImageView)v.findViewById(R.id.imagen));

                 titulo.setText(marker.getTitle());
                 direccion.setText(marker.getSnippet());

                 if(hash.get(marker) != null)
                    imagen.setImageDrawable(getResources().getDrawable(hash.get(marker)));

                 return v;

                }
                //..
                 @Override
                    public View getInfoContents(Marker marker) {
                        // TODO Auto-generated method stub
                        return null;
                    }
                 });
                }

    @Override
    public void onPause() {
        super.onPause();

     }
    @Override
    public void onDestroyView() {

        super.onDestroyView(); 
        Fragment fragment = (getFragmentManager().findFragmentById(R.id.map3));  
        if (fragment != null){
            getActivity().getSupportFragmentManager().beginTransaction()
            .remove(fragment)
            .commit();          
            }      
    }
}

fragment.xml

<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" >

   <fragment
        android:id="@+id/map3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>

</RelativeLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM