0

我正在寻找来自 xml 文件(RelativeLayout)的自定义标记的良好旅游或解决方案。

我有:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lay"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView
    android:layout_width="55dp"
    android:layout_height="65dp"
    android:src="@drawable/custom_marker" />

<TextView
    android:id="@+id/num_txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="5dp"
    android:gravity="center"
    android:text="20"
    android:textColor="#ce8223"
    android:textSize="25dp"
    android:textStyle="bold" />

    </RelativeLayout>

我如何将其用作标记?

4

1 回答 1

1

好吧,有一些解决方法可以将您的布局转换Drawable为可用作标记图像的布局:

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout view = (RelativeLayout)inflater.inflate(R.layout.your_layout, null);
view.setDrawingCacheEnabled(true);
view.setLayoutParams(new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 

view.buildDrawingCache();
//EDIT: updated drawable creation
Drawable d = new BitmapDrawable(context.getResources(), view.getDrawingCache().copy(Config.ARGB_8888, true)); //this drawable can be used as a marker

// **** EDIT2: don't forget to set bounds to your drawable
d.setBounds(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.setDrawingCacheEnabled(false);

正如您可能理解的那样,您需要存储Context指针才能获得LayoutInflater

希望能帮助到你

于 2013-01-16T00:27:59.323 回答