我在 128x128 智能 watch 屏幕上获得正确的布局时遇到问题。
这是一个有趣的问题,因为 watch 上的布局显然继承了其运行设备的屏幕密度。因此,当在平板电脑和手机上运行时, watch 上的布局元素的大小完全不同。
我的布局基于智能扩展 SDK 中的示例项目之一。
使用此 xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/smart_watch_control_width"
android:layout_height="@dimen/smart_watch_control_height"
android:id="@+id/rlayout"
>
<Button
android:id="@+id/imageViewUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:tag="button"
android:background="@drawable/up_selector" />
...more Buttons
</RelativeLayout>
初始化布局,SmartView只是一个扩展的LinearLayout
private LinearLayout createLayout() {
mSmartView = new SmartView(mContext);
mSmartView.setLayoutParams(new LayoutParams(WIDTH, HEIGHT));
LinearLayout sampleLayout = (LinearLayout) LinearLayout.inflate(
mContext, R.layout.xbmc, mSmartView);
sampleLayout.measure(WIDTH, HEIGHT);
Log.i(TAG, "layout width: " + sampleLayout.getMeasuredWidth()
+ " height: " + sampleLayout.getMeasuredHeight());
sampleLayout.layout(0, 0, sampleLayout.getMeasuredWidth(),
sampleLayout.getMeasuredHeight());
return sampleLayout;
}
createLayout的结果存储在mLayout中,然后使用以下方式绘制:
Bitmap bitmap = Bitmap.createBitmap(128, 128, BITMAP_CONFIG);
// Set default density to avoid scaling.
bitmap.setDensity(DisplayMetrics.DENSITY_DEFAULT);
Canvas canvas = new Canvas(bitmap);
mLayout.draw(canvas);
showBitmap(bitmap);
使用上面的 XML,在我的手机上我得到:
在平板电脑上,它看起来像这样:
此外,如果我对 XML 进行硬编码以使用图像的精确像素值,它看起来就像平板电脑版本。箭头图像为 44 像素,中间的图像稍大。
感觉我需要一种方法来以编程方式设置设备的密度,以强制其达到 watch 的密度。如何使布局在 watch 上看起来正确?我更喜欢一个可以避免对像素值进行硬编码的解决方案,因为这不是我们在 Android 上做事的方式。
最佳答案
尝试将可绘制对象存储在 res/drawable-nodpi 文件夹中,这应该会阻止 Android 应用密度。
这例如在可用的 8 拼图游戏中完成 here .
关于android - 索尼智能 watch 上的 XML 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10974245/