如何在android IconGenerator上居中文本 [英] How to center text on android IconGenerator

查看:33
本文介绍了如何在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".

如图书馆文档中所述:

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>

注意:: 布局必须包含一个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天全站免登陆