Android磨损类型检测-圆形或方形

Android磨损类型检测-圆形或方形,android,wear-os,Android,Wear Os,我很想知道磨损类型,无论是圆形还是方形,因为根据设备类型,我想对圆形和方形做出不同的看法。从中我了解到,在SDK-20android.view.view中,有一个新的监听器来检测设备类型,使用这个监听器,我试图获取设备类型,但logcat上没有打印任何内容。下面是我试过的代码 public class WearSplash extends Activity implements OnApplyWindowInsetsListener { private WatchViewStub _stub =

我很想知道磨损类型,无论是圆形还是方形,因为根据设备类型,我想对圆形和方形做出不同的看法。从中我了解到,在SDK-20
android.view.view中,有一个新的监听器来检测设备类型,使用这个监听器,我试图获取设备类型,但logcat上没有打印任何内容。下面是我试过的代码

public class WearSplash extends Activity implements OnApplyWindowInsetsListener {
private WatchViewStub _stub = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wear_splash);

    _stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    _stub.setOnApplyWindowInsetsListener(this);
    _stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {

        @Override
        public void onLayoutInflated(WatchViewStub mStub) {
            // here on the basis of device type i want to inflate views.
        }
    });

}
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
    System.out.println(" in testing phase");
    if (insets.isRound())
        System.out.println("in round");
    else
        System.out.println("in square");
    return null;
  }
}
xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/watch_view_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rectLayout="@layout/rect"
app:roundLayout="@layout/round"
tools:context="com.app.WearSplash"
tools:deviceIds="wear" >

</android.support.wearable.view.WatchViewStub>

圆形:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.WearSplash"
tools:deviceIds="wear_round" >

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:contentDescription="@string/hello_world" />

</RelativeLayout>

正方形:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.app.WearSplash"
tools:deviceIds="wear_square" >

<ImageView 
    android:id="@+id/image"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:contentDescription="@string/hello_world"
    android:background="@drawable/ic_launcher"/>

</LinearLayout>


我是新的服装开发人员,请告诉我哪里出了问题。帮助之手将不胜感激。

使用WatchViewStub为视图充气。稍后在布局文件中,您可以为每种手表类型指定如下布局。它将根据设备要求自动选择正确的布局

<android.support.wearable.view.WatchViewStub
    .
    .
    .
    app:rectLayout="@layout/rect_activity_my_wear"
    app:roundLayout="@layout/round_activity_my_wear"
    .
    .
    .
>

如果您需要以编程方式获取此信息,您可以为
setonapplywindowinsetslister()
添加一个侦听器。但是,要触发该操作,您还必须调用

在任何情况下,如果您只想知道这一点以在两种可选布局之间切换,您可以使用自动执行此操作的
WatchViewStub
类(docs)。

非官方方式(不使用WatchViewStub)-但对我来说,这更简单

只需复制shapewar.java类,并订阅屏幕形状检测事件
setOnShapeChangeListener()
或调用方法
shapewar.isRound()
(如果形状尚未确定,则可能抛出错误)或
shapewar。getShape()
-在相同的情况下,可能会导致
ShapeWear.SHAPE\u不确定

API 23介绍:


不确定这是否是问题所在,我也没有尝试过使用wear,但当我尝试在android studio中实现
OnApplyWindowInsettsListener
时,它被自动完成为
视图。OnApplyWindowInsettsListener
。因此,请尝试
公共类WearSplash扩展活动实现视图。OnApplyWindowInsetsListener
而不是
类WearSplash扩展活动实现OnApplyWindowInsetsListener
。已尝试,实际上,当我尝试在没有
视图的情况下在ApplicationWindowInsetsListener上实现
时,我遇到了一个错误。
部分。您尝试过调试吗?
public windowinserts-onapplywindowinserts(视图v,windowinserts)
被触发了吗?是的,我调试,从来没有调用过公共windowinserts-onapplywindowinserts(视图v,windowinserts-insets)。你能告诉我如何在我的代码中实现这一点来获取磨损类型吗?我认为没有必要调用
requestApplyInsets()
手动。我已经成功地在没有它的情况下调用了这个侦听器。@MaciejCiemięga在
watchviewsub
上,或者使用其他视图子类?@matiash:在任何视图上,比如
FrameLayout
TextView
等。@MaciejCiemięga我没有在其他视图上测试它,但是
watchviewsub
确实调用
requestApplyInsets()
手动。我的理论是,它在布局时会触发一次,因此如果您稍后添加侦听器,除非您再次请求,否则不会再次调用它。如果这个答案提供了更多的上下文,那就更好了。参考的链接很好,但是对于初学者(像我一样),很难看到如何在代码中使用它。