gpt4 book ai didi

android - 索尼智能 watch 上的 XML 布局

转载 作者:搜寻专家 更新时间:2023-11-01 08:09:45 25 4
gpt4 key购买 nike

我在 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,在我的手机上我得到了这个: Handset

在平板电脑上它看起来像这样: Tablet

此外,如果我对 XML 进行硬编码以使用图像的确切像素值,它看起来就像平板电脑版本。箭头图像为 44px,中间的稍大。

感觉我需要一种方法来以编程方式设置设备的密度,以将其强制为 watch 的密度。如何使布局在 watch 上看起来正确?我更喜欢一种可以避免对像素值进行硬编码的解决方案,因为这不是我们在 Android 上做事的方式。

最佳答案

尝试将您的可绘制对象存储在 res/drawable-nodpi 文件夹中,这应该会阻止 Android 应用密度。

这是例如在可用的 8 益智游戏中完成 here .

关于android - 索尼智能 watch 上的 XML 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10974245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com