【问题标题】:How to use setOnApplyWindowInsetsListener for Android Wear如何为 Android Wear 使用 setOnApplyWindowInsetsListener
【发布时间】:2014-09-22 14:56:07
【问题描述】:

我正在尝试检测我的应用何时在圆形 Wear 设备上运行,但我无法让 OnApplyWindowInsetsListener 从此 post 工作。

我可以将它附加到布局中的任何视图对象或布局本身吗?什么时候叫?我不能让听众开火。任何代码示例将不胜感激。

我在 OnResume 方法中创建并将这个 ImageView 添加到我的主布局中:

backgroundImage = new ImageView(this);
    backgroundImage.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {
            if (windowInsets.isRound()){
                Log.d("DEVELOPER", "......screen is round");
            }
            Log.d("DEVELOPER", "......windowinsets called");
            return null;
        }
    });

【问题讨论】:

    标签: android user-interface listener wear-os


    【解决方案1】:

    根据我的经验,并非所有视图都调用此侦听器。

    还要检查文档: https://developer.android.com/reference/android/view/View.html#onApplyWindowInsets(android.view.WindowInsets)

    使用WatchViewStub,您可以轻松获得它:

    WatchViewStub stub = (WatchViewStub) findViewById(R.id.stub);
    stub.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
                @Override
                public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
    
                    if(insets.isRound()) {
                        // Yes device is round
                    } 
                    return null;
                }
            });
    

    这里使用的布局:

    <android.support.wearable.view.WatchViewStub
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/stub"
            app:rectLayout="@layout/rect_layout"
            app:roundLayout="@layout/round_layout"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-01
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多