如何在Android IconGenerator上居中显示文字 [英] How to center text on android IconGenerator

查看:120
本文介绍了如何在Android IconGenerator上居中显示文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用放置在地图上的许多标记来开发应用程序,并且正在使用自定义的ClusterRenderer来显示它们. 问题是我无法在自定义标记图标的中心绘制群集的大小,请参见所附的屏幕截图. 我尝试将contentPadding添加到IconGenerator,但由于显示的数字不断变化,仍然没有运气.您能帮我将文字放在生成的图标上居中吗?

I'm developing an app using lots of markers placed on the map, and I'm using a custom ClusterRenderer to show them. Problem is that I can't draw the cluster's size in the center of the custom marker icon, please see attached screenshot. I've tried adding contentPadding to the IconGenerator, but still no luck, because of the changing number of digits shown. Could you please help me center the text on the generated icon?

代码:

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

推荐答案

UPDATE

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

UPDATE

starting Apr 1, 2016 a prefix has been added to the Resources of the library so the id="text" has been changed to "amu_text".

如图书馆文档中所述:

setContentView public void setContentView(View contentView)
设置图标的子视图.
如果视图包含一个 ID为"amu_text"的TextView,例如 setTextAppearance(Context,int)和makeIcon(String)将运行 在该TextView上.

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为

with the layout cluster_view as :

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

note ::布局必须包含一个id ="amu_text"的文本视图,图标生成器才能接受它,并控制布局中所需的所有位置.

note :: the layout must contain one text view with an id = "amu_text" in order for the icon generator to accept it , manipulate all the positioning you want in the layout .

这篇关于如何在Android IconGenerator上居中显示文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆