如何将 android IconGenerator 上的文本居中
How to center text on android IconGenerator
我正在使用放置在地图上的大量标记开发一个应用程序,并且我正在使用自定义 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));
}
更新
自 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" 的文本视图,以便图标生成器接受它,在布局中操作您想要的所有定位。
我正在使用放置在地图上的大量标记开发一个应用程序,并且我正在使用自定义 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));
}
更新
自 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" 的文本视图,以便图标生成器接受它,在布局中操作您想要的所有定位。