2012-04-21 43 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")。

相关问题