我在 Android Wear 项目中实现了 onApplyWindowInsets 回调。在该方法中,它调用 isRound() 来确定代码是在圆形还是矩形 Wear 智能手表上运行。这个回调是从圆形磨损模拟器中调用的。(虽然 isRound() 返回 false,这是错误的。)但问题是我的 Samsung Gear Live 从未调用过 onApplyWindowInsets 回调。因此,如果从 onApplyWindowInsets 膨胀视图,则视图不会膨胀。有没有人让 onApplyWindowInsets 在真实设备上触发?似乎不能保证。如果是这样,那么如何根据屏幕类型膨胀不同的视图?
4 回答
在您的布局中,您可以为圆形/矩形屏幕设置特定的“子”布局:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.wearable.view.WatchViewStub
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/stub"
app:rectLayout="@layout/rect_layout"
app:roundLayout="@layout/round_layout"/>
</FrameLayout>
如果你对 rect_layout 和 round_layout 使用不同的 id,你可以在 onCreate 上检查它,像这样:
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.main_activity);
WatchViewStub stub = (WatchViewStub) findViewById(R.id.stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
mRectBackground = (RelativeLayout) findViewById(R.id.rect_layout);
mRoundBackground = (RelativeLayout) findViewById(R.id.round_layout);
}
});
}
mRectBackground 或 mRoundBackground 将是非空的,而不是两者。此处的文档:https ://developer.android.com/training/wearables/apps/layouts.html#UiLibrary
其他人尝试使用使用 setOnApplyWindowInsetsListener 的策略,如下所示:https ://github.com/xamarin/monodroid-samples/blob/master/wear/GridViewPager/GridViewPager/MainActivity.cs 。它对我不起作用。
onApplyWindowInsets
实际上需要开发者在监听器中调用WatchViewStub
'方法。onApplyWindowInsets
例如:
WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@Override
public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {
//////////////////////////////////////////////////
// IMPORTANT - the following line is required
stub.onApplyWindowInsets(windowInsets);
// Set the instance variable to say whether this is round...
mIsRound = windowInsets.isRound();
return windowInsets;
}
});
尽管乍一看可能并不直观,但这允许开发人员在onApplyWindowInsets
他们选择时覆盖默认实现。
文档View.OnApplyWindowInsetsListener
声明:“侦听器可以选择调用参数 View 的 onApplyWindowInsets 方法以将 View 的正常行为作为其自身的一部分应用。”
谢谢艾德!你聪明的技术似乎已经解决了这个问题。(我说“似乎”,因为在将 mRoundBackground 设置为非空值的真实圆形设备上测试代码之前,我无法确定。)
现在有三种 Android Wear 形状(矩形/方形、圆形 w/下巴和全圆形),我添加了两个全局值来查找屏幕形状,然后修改回调如下:
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
//mRectBackground = (RelativeLayout) findViewById(R.id.LinearLayout1);
mRoundBackground = (RelativeLayout) findViewById(R.id.LinearLayout2);
if (mRoundBackground != null) round = true;
else round = false;
WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = window.getDefaultDisplay();
//Added android.graphics.Point to imports for this next section
Point point = new Point (0,0);
display.getSize(point);
scrnHeight = point.y;
scrnWidth = point.x;
if (round && scrnWidth == scrnHeight) chin = false;
else chin = true;
//round = true; //For testing on the emulator!!!!!!
}
});
有谁知道在 Moto360 上检测下巴的更好方法?
更新:刚刚在 Moto 360 上运行此代码,但此技术未检测到圆形屏幕。所以现在我只是检测高度是否等于宽度。但当更多的 Android Wear 设备出现时,这对于未来来说是一个糟糕的技术。所以问题仍然存在,如何在真正的 Android Wear 设备上确定圆形屏幕?
非官方方式(不使用 WatchViewStub)——但对我来说这要容易得多。 https://github.com/tajchert/ShapeWear
只需复制 ShapeWear.java 类,并订阅屏幕形状检测事件setOnShapeChangeListener()
或调用方法ShapeWear.isRound()
(如果形状尚未确定,可能会抛出错误)或ShapeWear. getShape()
- 这可能会导致ShapeWear.SHAPE_UNSURE
相同的情况。