android - 如何在android IconGenerator上将文本居中

标签 android bitmap google-maps-markers google-maps-android-api-2 markerclusterer

我正在使用放置在 map 上的大量标记开发一个应用程序,并且我正在使用自定义 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));
}

enter image description here

最佳答案

更新

自 2016 年 4 月 1 日起,图书馆资源中添加了一个前缀 所以 id="text"已更改为 "amu_text"


如库文档中所述:

setContentView public void setContentView(View contentView)
Sets the child view for the icon.
If the view contains a TextView with the id "amu_text", operations such as setTextAppearance(Context, int) and makeIcon(String) will operate upon that 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);
}

布局 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/amu_text"
        android:layout_marginTop="13dp"
        android:gravity="center" />
</LinearLayout>

注意:布局必须包含一个带有 id = "amu_text"的 TextView ,以便图标生成器接受它,并在布局中操作您想要的所有位置。

关于android - 如何在android IconGenerator上将文本居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32836595/

相关文章:

Android AsyncTask 不调用 onPostExecute

android - 在 Android 谷歌地图 v2 中打开自定义信息窗口

google-maps - 标记管理器不显示标记

php - Google map v3 api 中的标记不再访问内容

java - 将带有 2 个库的 Eclipse 项目导入到 Android Studio

android - 唤醒屏幕并在锁定屏幕上显示对话框

android - SQLite 数据库在 Android 中重新打开

c# - 在 C# 中读取 PPM 图像

Android HashMap 序列化/反序列化

android - 如何从文件管理器中获取位图?