android - 如何在 Android Wear 上检测圆形屏幕 - onApplyWindowInsets 未在设备上调用

标签 android rounding screens

我已经在 Android Wear 项目中实现了 onApplyWindowInsets 回调。在该方法中,它调用 isRound() 来确定代码是在圆形还是矩形的 Wear 智能 watch 上运行。此回调是从圆形磨损模拟器中调用的。 (虽然 isRound() 返回 false,这是错误的。)但问题是 onApplyWindowInsets 回调从未从我的 Samsung Gear Live 中调用。因此,如果从 onApplyWindowInsets 膨胀 View ,则 View 不会膨胀。有人让 onApplyWindowInsets 在真实设备上触发吗?看来是不能保证了。如果是这样,那么如何根据屏幕类型扩展不同的 View 呢?

最佳答案

在布局中,您可以为圆形/矩形屏幕设置特定的“子”布局:

 <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 。它对我不起作用。

关于android - 如何在 Android Wear 上检测圆形屏幕 - onApplyWindowInsets 未在设备上调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25495005/

相关文章:

java - Android 10 及更高版本上的即时应用内更新会在安装/重启后关闭应用

Android 音乐文件选择器 Intent 问题

android - Android 程序上传.json 文件并接收.json 响应

node.js - 圆形图像角 GM ImageMagic,传递选项

c# - 为什么 C# round 和 SQL round 函数产生不同的输出?

java - Android Studio ArrayAdapter 要求资源 ID 为 TextView

swift - 将 Double 舍入到最接近的 10

java - JFrame多屏

Android:保存屏幕状态(初学者)

android - Kivy (Python) 格式化问题