我正在寻找一种技术来确定Java Wear设备屏幕是圆形还是矩形。请注意,这不仅仅是布局;我的代码实际上需要知道它处理的是哪个形状,因为它们的处理方式不同。根据我在网上的代码示例中看到的,两种不同的方法应该是可能的 - 但我一直无法让他们中的任何一个人工作。我会将它们包含在这里以消除它们的运行,或者可能的故障排除(如果任何人都可以看到它们的问题)。请不要将我引荐给另一个SO职位,只是重申在这里不适合我的解决方案。以编程方式确定Android Wear中的屏幕形状
请注意,这里的所有代码都在手表上运行。另外,我仍然在使用Eclipse,FWIW。
我见过的最简单的方法是在我的布局中添加一个onApplyWindowInsets()
侦听器。在我的onCreate()方法
view.setOnApplyWindowInsetsListener(this);
:所以我创建了一个监听器,看起来像这样:
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
if (insets.isRound()) {
displayShape = "round";
} else {
displayShape = "rectangular";
}
return null;
}
,并把它添加到我的布局与代码根视图这样。看起来没问题 - 但听众永远不会被叫到。我还发现一些建议,说我需要手动调用它,因此:
view.requestApplyInsets();
但这似乎没有任何区别。我已经尝试过将它放在不同的视图,不同的生命周期方法等等,但从来没有看到它实际上被我的应用程序调用。这是在我的LG G手表上运行,BTW。
第二种方法是一种黑客攻击,它基于已发布的WatchViewStub
辅助类。我通过篮球跃升得到导入到Eclipse项目中的可穿戴的支持库,然后添加以下到我的根布局:
<android.support.wearable.view.WatchViewStub
android:id="@+id/watch_view_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rectLayout="@layout/rect"
app:roundLayout="@layout/round"
/>
创造rect.xml
这样:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout_type"
android:text="rectangular"
/>
和round.xml
这样:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout_type"
android:text="round"
/>
最后,在我的onCreate()
我添加下面的Java代码:
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
TextView layoutType = (TextView) findViewById(R.id.layout_type);
displayShape = layoutType.getText().toString();
}
});
这是一段很长的路,但它应该工作,对吧?没有那么多...... displayShape
始终设置为"rectangular"
,表明它始终使用rect.xml
,即使在轮循模拟器上运行也是如此。 [我没有经过全面筛选的硬件,现在就试用它。]
那么,有没有人看到我在这两种方法中哪一种出错?或者你能建议第三种方法吗?
感谢您的反馈。我会研究这个主题,看看在这里可以做些什么。 – 2014-10-16 06:27:19
对于资源限定符来说,手表显示更多的不仅仅是圆形和方形。 Moto 360在圆形显示屏的底部有一个插图,而圆形模拟器没有插页。所以他们在那里也不一样。所以我们使用WindowInsets https://developer.android.com/reference/android/view/WindowInsets.html来编码显示器之间的差异。 – 2014-10-16 06:29:04
这绝对是我没有调用“OnApplyWindowInsetsListener.onApplyWindowInsets()”方法时遇到的问题。 根本原因是我正在构建一个基于'android:Theme'使用主题'GdxTheme'的_libgdx_项目。当我将'GdxTheme'改为从'@android:style/Theme.DeviceDefault'继承时,调用了insets回调方法。 这里是固定版本的'GdxTheme': ''style name =“GdxTheme”parent =“@ android:style/Theme.DeviceDefault”>' – outofcoffee 2014-12-21 19:48:22