膨胀视图未从 xml 获取尺寸

Inflated view does not get dimmensions from xml

我正在尝试将自定义视图添加到 google 地图 Marker:

  1. 我给布局充气。 <- 这就是问题所在
  2. 然后将其转换为位图。
  3. 生成标记。


所以我有布局 home_marker_container.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/home_map_marker_size"
    android:layout_height="@dimen/home_map_marker_size"
    android:background="@color/black"
    android:id="@+id/frame">


    <TextView
        android:layout_width="12dp"
        android:layout_height="12dp"
        android:text="5"
        android:textSize="10dp"
        android:background="@color/white"
        android:layout_centerInParent="true"
        android:gravity="center" />

</RelativeLayout>

我用

给它充气
ViewGroup home_marker_container = (ViewGroup) layoutInflater.inflate(R.layout.home_marker_container, null, false);


然后将视图转换为位图:

所以我用 Google Maps Android API Utility Library via IconGenerator which basically transforms View to Bitmap:

将它添加到标记中
iconGenerator.setContentView(home_marker_container);
iconGenerator.setContentPadding(horizontal_padding, vertical_padding, horizontal_padding, vertical_padding);
Bitmap iconBitmap = iconGenerator.makeIcon();

之后我意识到充气并没有像我预期的那样工作,因为我在 inflate(int resource, ViewGroup root, boolean attachToRoot) 中将 nullViewGroup root 参数,因此

android:layout_width="@dimen/home_map_marker_size"
android:layout_height="@dimen/home_map_marker_size"

不是取自 xml。 为什么以及如何解决?

因为没有根,according to docs,根是从 inflate 返回的,所以我手动将大小设置为根:

View markerView;
markerView = layoutInflater.inflate(R.layout.marker1, null);
markerView.setLayoutParams(new ViewGroup.LayoutParams(80, 80));;