如何在android IconGenerator上居中显示文本
我正在开发一个应用程序,使用地图上的许多标记,并使用自定义ClusterRenderer来显示它们。 问题是,我无法在自定义标记图标的中心绘制集群的大小,请参阅所附的屏幕截图。 我曾尝试将contentPadding添加到IconGenerator,但仍然没有成功,因为显示的位数不断变化。你能帮我把生成的图标上的文字居中吗 代码:如何在android IconGenerator上居中显示文本,android,bitmap,google-maps-markers,google-maps-android-api-2,markerclusterer,Android,Bitmap,Google Maps Markers,Google Maps Android Api 2,Markerclusterer,我正在开发一个应用程序,使用地图上的许多标记,并使用自定义ClusterRenderer来显示它们。 问题是,我无法在自定义标记图标的中心绘制集群的大小,请参阅所附的屏幕截图。 我曾尝试将contentPadding添加到IconGenerator,但仍然没有成功,因为显示的位数不断变化。你能帮我把生成的图标上的文字居中吗 代码: IconGenerator clusterIconGenerator=新的IconGenerator(上下文); clusterIcon=context.getRes
IconGenerator clusterIconGenerator=新的IconGenerator(上下文);
clusterIcon=context.getResources().getDrawable(R.drawable.map_cluster);
ClusterCongenerator.setBackground(clusterIcon);
@凌驾
在呈现群集之前受保护的void(群集群集、标记选项标记选项){
位图clusterIcon=clusterIconGenerator.makeIcon(String.valueOf(cluster.getSize());
图标(BitmapDescriptorFactory.fromBitmap(clusterIcon));
}
更新
从2016年4月1日起,图书馆的资源中添加了前缀
因此,id=“text”已更改为“amu\u text”
如图书馆文件所述: setContentView public void setContentView(视图contentView)
设置图标的子视图。
如果视图包含 id为“amu_text”的文本视图,操作如下 setTextAppearance(上下文,int)和makeIcon(字符串)将运行 在那个文本视图上
@覆盖
在呈现群集之前受保护的void(群集群集、标记选项标记选项){
IconGenerator TextMarkerGen=新IconGenerator(上下文);
可拉拔标记;
int ClusterSize=cluster.getSize();
marker=context.getResources().getDrawable(R.drawable.cluster_red);
TextMarkerGen.后退地面(标记);
LayoutInflater myInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
视图活动视图=myInflater.inflate(R.layout.cluster\u视图,null,false);
TextMarkerGen.setContentView(activityView);
TextMarkerGen.makeIcon(String.valueOf(cluster.getSize());
BitmapDescriptor icon=BitmapDescriptorFactory.fromBitmap(TextMarkerGen.makeIcon());
标记选项。图标(图标);
}
布局群集视图为:
<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”的文本视图,为了让图标生成器接受它,请在版面中操纵您想要的所有位置 非常感谢你的回答,我错过了文档的这一部分,你是救命恩人!虽然这样,文本仍然出现在集群的左上角,因为布局没有应用标记图标的布局参数或其他东西,但至少我能够自定义它。我的解决方法是将布局更改为FrameLayout,并添加一个以标记图像为背景的视图,这样布局将扩展到正确的大小。的确,textview将隐藏此视图,并将额外的元素加载到内存中,但它解决了我的问题,再次感谢!如果我使用一个形状作为集群图标的背景,因此我没有集群的任何布局,这是怎么回事?@KasparTr我从未尝试过形状,但你可以设置背景图像。至于布局,如果您打算更好地控制集群上写的内容,您必须创建一个布局。对于我来说,Textview id=“text”不起作用。根据文件,ID应为“amu_文本”。我的答案已随库中的新更改而更新,谢谢
@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);
}
<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>