如何在Android Iconnenerator上居中文本

Ana*_*zer 9 android bitmap google-maps-markers markerclusterer google-maps-android-api-2

我正在开发一个使用大量放置在地图上的标记的应用程序,我正在使用自定义ClusterRenderer来显示它们.问题是我无法在自定义标记图标的中心绘制群集的大小,请参阅附带的屏幕截图.我已经尝试将contentPadding添加到IconGenerator,但仍然没有运气,因为显示的位数变化.你能帮我把文字集中在生成的图标上吗?

码:

 IconGenerator clusterIconGenerator = new IconGenerator(context);
 clusterIcon = context.getResources().getDrawable(R.drawable.map_cluster);
 clusterIconGenerator.setBackground(clusterIcon);

@Override
protected void onBeforeClusterRendered(Cluster<MyType> cluster, MarkerOptions markerOptions) {

    Bitmap clusterIcon = clusterIconGenerator.makeIcon(String.valueOf(cluster.getSize()));
    markerOptions.icon(BitmapDescriptorFactory.fromBitmap(clusterIcon));
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Sar*_*her 16

UPDATE

从2016年4月1日开始,在库的资源中添加了一个前缀,因此id ="text"已更改为"amu_text".


如图书馆文档中所述:

setContentView public void setContentView(View contentView)
设置图标的子视图.
如果视图包含带有"text"的TextView,则setTextAppearance(Context,int)和makeIcon(String)等操作将对该TextView进行操作.

@Override
protected void onBeforeClusterRendered(Cluster<Dashboard_Marker> cluster, MarkerOptions markerOptions) {
    IconGenerator TextMarkerGen = new IconGenerator(context);
    Drawable marker;
    int ClusterSize = cluster.getSize();

    marker = context.getResources().getDrawable(R.drawable.cluster_red);
    TextMarkerGen.setBackground(marker);

    LayoutInflater myInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View activityView = myInflater.inflate(R.layout.cluster_view, null, false);

    TextMarkerGen.setContentView(activityView);
    TextMarkerGen.makeIcon(String.valueOf(cluster.getSize()));

    BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(TextMarkerGen.makeIcon());
    markerOptions.icon(icon);
}
Run Code Online (Sandbox Code Playgroud)

布局cluster_view为:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerInParent="true"
    android:layout_centerVertical="true"
    android:weightSum="1">

    <TextView
        android:layout_width="61dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:textColor="#000000"
        android:id="@+id/text"
        android:layout_marginTop="13dp"
        android:gravity="center" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

note ::布局必须包含一个带有id ="text"的文本视图,以便图标生成器接受它,操纵布局中所需的所有定位.

  • 非常感谢你的回答,我错过了文档的这一部分,你是一个救星!虽然这样文本仍然出现在群集的左上角,因为布局没有应用标记图标的布局参数或其他东西,但至少我能够自定义它.我的解决方法是将布局更改为FrameLayout并添加带有标记图像的视图作为背景,因此布局将扩展到正确的大小.确实,textview将隐藏这个View,并且一个额外的元素将被加载到内存中,但它解决了我的问题,再次感谢! (2认同)
  • 对我来说,它不适用于Textview id ="text".根据文档,ID应为"amu_text".https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/ui/IconGenerator.java (2认同)