【问题标题】:How to center text on android IconGenerator如何在 android IconGenerator 上居中文本
【发布时间】:2015-12-26 11:57:22
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    更新

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


    如库文档中所述:

    setContentView public void setContentView(View contentView)
    设置图标的子视图。
    如果视图包含 id为“amu_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);
    }
    

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

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

    【讨论】:

    • 非常感谢您的回答,我错过了这部分文档,您是救生员!虽然这样文本仍然出现在集群的左上角,因为布局没有应用标记图标的布局参数或其他东西,但至少我能够自定义它。我的解决方法是将布局更改为 FrameLayout 并添加一个带有标记图像作为背景的视图,因此布局将扩展到正确的大小。确实,textview 会隐藏这个 View,并且会在内存中加载一个额外的元素,但它解决了我的问题,再次感谢!
    • 如果我使用一个形状作为该集群图标的背景,因此我没有任何集群布局?
    • @KasparTr 我从未尝试过形状,但您可以设置背景图像。至于布局,如果您打算更好地控制集群上写入的内容,则必须创建一个
    • 对我来说,它不适用于 Textview id = "text"。根据文档,ID 应该是“amu_text”。 github.com/googlemaps/android-maps-utils/blob/master/library/…
    • 我的答案已经随着图书馆的新变化而更新,谢谢
    猜你喜欢
    • 2014-09-02
    • 2011-06-14
    • 1970-01-01
    • 2011-11-07
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    相关资源
    最近更新 更多