除了android中的硬编码值之外,还有其他设计UI的方法吗
当我浏览了几个android示例时,我发现一些值是硬编码的 例如:除了android中的硬编码值之外,还有其他设计UI的方法吗,android,android-layout,layout,user-interface,Android,Android Layout,Layout,User Interface,当我浏览了几个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"
<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中是正确的方式吗?它对各种屏幕大小的设备有影响吗?最好使用“dp”单元而不是“px”
DP将根据屏幕大小而不是PX进行调整
请参见最好使用“dp”单元而不是“px”
DP将根据屏幕大小而不是PX进行调整
看
在此图像视图中,对于我的自定义布局,值是硬编码的。。如何避免这些硬编码
首先,通常不应使用px
作为维度,因为硬件像素的大小可能会根据屏幕密度而变化。使用dp
或其他测量单位(例如mm
)
第二,如果您有计划重用的维度,或者您只是希望在一个地方收集它们的值,请使用。然后,您的版面将引用这些资源(例如,android:layout_marginTop=“@dimen/something”
)
在此图像视图中,对于我的自定义布局,值是硬编码的。。如何避免这些硬编码
首先,通常不应使用px
作为维度,因为硬件像素的大小可能会根据屏幕密度而变化。使用dp
或其他测量单位(例如mm
)
第二,如果您有计划重用的维度,或者您只是希望在一个地方收集它们的值,请使用。然后,您的版面将引用这些资源(例如,android:layout_marginTop=“@dimen/something”
)。值应为“dp”或“dpi”,并相应调整不同的安卓设备
这将有助于您:值应为“dp”或“dpi”,并且它将相应地调整不同的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>
<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);