簡體   English   中英

在標記地圖android上居中文本

[英]center text on a marker map android

我不知道要解決這個問題,我已經生成了帶有可繪制對象的圖標和帶有價格的文本,我無法將文本置於標記的中間[我的文本位於地球的左側,我需要居中]

public static Bitmap createTextMarker (Context context, String text) {

    final Resources res = context.getResources();

   final int markerPadding = res.getDimensionPixelOffset(
      R.dimen.activity_maps_marker_padding);

    final IconGenerator iconGenerator = new IconGenerator(context);
    iconGenerator.setBackground(res.getDrawable(R.drawable.ic_marker_price1));
    iconGenerator.setTextAppearance(R.style.ParkingMarkerText);


  iconGenerator.setContentPadding(markerPadding,
       markerPadding + (markerPadding / 3),
     markerPadding, 0);
    return iconGenerator.makeIcon(text);

}

您應該更改Java代碼只是為了調用XML,而不要嘗試這樣做。 正如Pradep所說,使用以下代碼創建marker.xml:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/MarkerIcon"
    android:layout_width="fill_parent"
    android:layout_height="250dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:scaleType="fitXY"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/priceTag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="20dp"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

只需更改android:layout_marginTop="20dp"android:layout_height="250dp"即可查看哪個值更適合您

將此添加到.java部分

View markerView = mActivity.getLayoutInflater().inflate(R.layout.marker, null);
iconGenerator.setContentView(markerView);

並刪除iconGenerator.setBackground(res.getDrawable(R.drawable.ic_marker_price1));

在文件夾“ layout”中創建文件“ custom_marker_small.xml”:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/MarkerIcon"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:src="@drawable/group_location_picker_1_5"/>

    <TextView
        android:id="@+id/priceTag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:paddingTop="7dp"
        android:text="77"
        android:textColor="@color/white"
        android:textSize="12sp"/>

</RelativeLayout>



 IconGenerator generator = new IconGenerator(context);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View markerView = inflater.inflate(R.layout.custom_marker_small, null);
            generator.setContentView(markerView);
            generator.setBackground(null);

    Bitmap icon = generator.makeIcon();
            markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));

嘗試這個,

使用這個marker.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/image"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/pin_new" />

        <TextView
            android:id="@id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_gravity="center"
            android:paddingBottom="7dp"
            android:textSize="12sp"
            android:textStyle="bold"
            android:textColor="@color/orange" />
    </RelativeLayout>

</FrameLayout>

並添加以下代碼:

View markerView = mActivity.getLayoutInflater().inflate(R.layout.marker, null);
iconGenerator.setContentView(markerView);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM