简体   繁体   中英

center text on a marker map android

I have no idea to solve this , i have my icon generated wit a drawable and a text with prices, i cant center the text in the middle of a marker [icons with text in left of the globe i need center]

public static Bitmap createTextMarker (Context context, String text) {

    final Resources res = context.getResources();

   final int markerPadding = res.getDimensionPixelOffset(
      R.dimen.activity_maps_marker_padding);

    final IconGenerator iconGenerator = new IconGenerator(context);
    iconGenerator.setBackground(res.getDrawable(R.drawable.ic_marker_price1));
    iconGenerator.setTextAppearance(R.style.ParkingMarkerText);


  iconGenerator.setContentPadding(markerPadding,
       markerPadding + (markerPadding / 3),
     markerPadding, 0);
    return iconGenerator.makeIcon(text);

}

You should change your Java code just to call to a XML instead of try that. As Pradep said, create a marker.xml with this code:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/MarkerIcon"
    android:layout_width="fill_parent"
    android:layout_height="250dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:scaleType="fitXY"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/priceTag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="20dp"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

Just change android:layout_marginTop="20dp" and android:layout_height="250dp" to see which value suits you better

Add this to the .java part

View markerView = mActivity.getLayoutInflater().inflate(R.layout.marker, null);
iconGenerator.setContentView(markerView);

And remove iconGenerator.setBackground(res.getDrawable(R.drawable.ic_marker_price1));

in the folder "layout" create a file "custom_marker_small.xml":

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

    <ImageView
        android:id="@+id/MarkerIcon"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:src="@drawable/group_location_picker_1_5"/>

    <TextView
        android:id="@+id/priceTag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:paddingTop="7dp"
        android:text="77"
        android:textColor="@color/white"
        android:textSize="12sp"/>

</RelativeLayout>



 IconGenerator generator = new IconGenerator(context);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View markerView = inflater.inflate(R.layout.custom_marker_small, null);
            generator.setContentView(markerView);
            generator.setBackground(null);

    Bitmap icon = generator.makeIcon();
            markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));

try this,

use this marker.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/image"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/pin_new" />

        <TextView
            android:id="@id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_gravity="center"
            android:paddingBottom="7dp"
            android:textSize="12sp"
            android:textStyle="bold"
            android:textColor="@color/orange" />
    </RelativeLayout>

</FrameLayout>

and add this code:

View markerView = mActivity.getLayoutInflater().inflate(R.layout.marker, null);
iconGenerator.setContentView(markerView);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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