2015-10-19 31 views
0

在我的谷歌地圖應用程序中,我爲不同的位置使用自定義標記。我成功實現了這一點,但我需要動態增加自定義標記的大小。我用下面的代碼動態增加谷歌地圖中自定義標記的大小

imgView.getLayoutParams().height = 200; 
imgView.getLayoutParams().width = 200; 

代碼工作正常,但在nexus4和5標記是非常大的一些設備。請幫我解決這個問題

自定義標記的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:layout_width="36dp" 
     android:layout_height="36dp" 
     android:src="@drawable/marker" /> 

    <TextView   
     android:layout_width="36dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="3dp" 
     android:gravity="center" 
     android:textColor="#FFFFFF" 
     android:textSize="14sp" 
     android:textStyle="bold" /> 

</RelativeLayout> 
+0

如何在xml中設置layoutparams?重量也許呢? –

+0

請檢查我的自定義標記XML – user1517638

回答

0

隨着

imgView.getLayoutParams().height = 200; 

是否指定了新的高度像素,忽略當前設備的像素密度。你不應該這樣做。

您可以使用當前的方法來放大圖像,例如,

int height = imgView.getLayoutParams().height; 
imgView.getLayoutParams().height = height * 2; 

使它們變大2倍。不管設備的像素密度如何,圖像都是硬編碼的200px,如果是70px,則圖像爲140px,如果是100px,則爲200px。
這將很好地適應當前的設備。

還可以帶有DP工作,通過指定DP值,然後將其轉換爲像素:通過平移72成像素適量當前設備

int dp = 72; 
int height = imgView.getLayoutParams().height; 
imgView.getLayoutParams().height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics()); 

這將高度設置爲72 。