2012-04-21 64 views
0

當我通過幾Android的例子去,因爲我找到了一些值硬編碼,是否有設計比硬編碼值以外的UI任何替代的Android

對於如:

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="22px" 
    android:layout_height="22px" 
    android:layout_marginLeft="4px" 
    android:layout_marginRight="10px" 
    android:layout_marginTop="4px" 
    android:src="@drawable/ic_launcher" > 
</ImageView> 

在這個圖像視圖值是硬編碼,我的自定義佈局..如何避免這些類型的硬編碼? 這是Android中的正確方法嗎?它對各種屏幕尺寸設備有任何影響嗎?

回答

2

您需要閱讀一些開發者文檔:

http://developer.android.com/guide/practices/screens_support.html http://developer.android.com/guide/practices/screens_support.html#screen-independence

NO:

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="22px" 
    android:layout_height="22px" 
    android:layout_marginLeft="4px" 
    android:layout_marginRight="10px" 
    android:layout_marginTop="4px" 
    android:src="@drawable/ic_launcher" > 
</ImageView> 

以上也不會在屏幕

很好地擴展

是:

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="22dip" 
    android:layout_height="22dip" 
    android:src="@drawable/ic_launcher" > 
</ImageView> 

以上將擴展其像素每設備 '獨立'

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher" > 
</ImageView> 

上面將相對它自己繪製到屏幕尺寸

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="0dip" 
    android:layout_weight="1" 
    android:layout_height="22dip" 
    android:src="@drawable/ic_launcher" > 
</ImageView> 

上面會繪製自身相對於屏幕大小和

ImageView imageView = new ImageView(this); 
     imageView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     imageView.setImageDrawable(R.drawable.background); 

     layout.addView(imageView); 

以上就是編程

創建屏幕上的其他意見
0

在這種圖像視圖值是硬編碼,爲我的自定義佈局..如何避免這類硬編碼的?

首先,通常您不應該使用px作爲尺寸,因爲硬件像素可能因屏幕密度而異。使用dp或其他計量單位(例如mm)。其次,如果您有尺寸要重複使用,或者您只是希望在一個位置收集其值,請使用dimension resources。然後,您的佈局將引用這些資源(例如,android:layout_marginTop="@dimen/something")。

相關問題