Android Wear类型检测 - 圆形或方形

时间:2014-07-23 10:06:05

标签: android wear-os

我很想知道磨损类型,无论是圆形还是方形,因为根据设备类型我想要制作圆形和方形的不同视图。从This Post开始我知道在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>

我是新手,让我知道我哪里出错了。帮助将受到高度赞赏。

4 个答案:

答案 0 :(得分:8)

API 23介绍:

isScreenRound()

答案 1 :(得分:7)

非官方的方式(不使用WatchViewStub) - 但对我来说这更容易。 https://github.com/tajchert/ShapeWear

只需复制ShapeWear.java类,订阅屏幕形状检测事件setOnShapeChangeListener()或调用方法ShapeWear.isRound()(可以抛出错误,形状尚未确定)或ShapeWear. getShape() - 哪个可以导致ShapeWear.SHAPE_UNSURE处于同样的情况。

答案 2 :(得分:1)

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

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

答案 3 :(得分:1)

如果您需要以编程方式获取此信息,那么添加setOnApplyWindowInsetsListener()的侦听器是正确的。但是,为了触发这一点,您必须致电requestApplyInsets()

在任何情况下,如果您只想知道这在两个备用布局之间切换,您可以使用自动执行此操作的WatchViewStub类(文档here)。