我有一个自定义地图标记,我想根据标记的文本进行缩放。我想检索这个标记的宽度。我面临的问题是,当应用程序首次启动时,宽度值为 0。我得到宽度太早了。但我无法让 ViewTreeObserver 实际工作。我做错了什么,是否有可能我没有参考正确的观点?
// custom marker
mCustomMarkerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_map_marker, null);
tvCustomMarkerTitle = (FontedTextView) mCustomMarkerView.findViewById(R.id.tv_marker_title);
ivCustomMarker = (ImageView) mCustomMarkerView.findViewById(R.id.iv_marker_bg);
rlCustomMarkerWrapper = (RelativeLayout) mCustomMarkerView.findViewById(R.id.rl_custom_marker_wrapper);
rlCustomMarkerWrapper.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// do some work
}
});
XML 如下所示
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/rl_custom_marker_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- custom marker image -->
<ImageView
android:id="@+id/iv_marker_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/custom_marker"/>
<!-- custom marker title -->
<TextView
android:id="@+id/tv_marker_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="@dimen/font_size_14"/>
</RelativeLayout>
</FrameLayout>
提前谢谢你!