繁体   English   中英

在标记地图android上居中文本

[英]center text on a marker map android

我不知道要解决这个问题,我已经生成了带有可绘制对象的图标和带有价格的文本,我无法将文本置于标记的中间[我的文本位于地球的左侧,我需要居中]

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);

}

您应该更改Java代码只是为了调用XML,而不要尝试这样做。 正如Pradep所说,使用以下代码创建marker.xml:

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

只需更改android:layout_marginTop="20dp"android:layout_height="250dp"即可查看哪个值更适合您

将此添加到.java部分

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

并删除iconGenerator.setBackground(res.getDrawable(R.drawable.ic_marker_price1));

在文件夹“ layout”中创建文件“ 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));

尝试这个,

使用这个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>

并添加以下代码:

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

暂无
暂无

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

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