【问题标题】:Android Wear type detection - Round Or SquareAndroid Wear 类型检测 - 圆形或方形
【发布时间】:2014-07-23 10:06:05
【问题描述】:

我很想知道佩戴类型是圆形还是方形,因为我想根据设备类型对圆形和方形做出不同的看法。从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>

我是新穿开发,让我知道我哪里出错了。我们将非常感谢您提供帮助。

【问题讨论】:

  • 不确定是不是这个问题,我也没有尝试过穿,但是当我尝试在android studio中实现OnApplyWindowInsetsListener时,它自动完成为View.OnApplyWindowInsetsListener。所以你能不能试试public class WearSplash extends Activity implements View.OnApplyWindowInsetsListener而不是class WearSplash extends Activity implements OnApplyWindowInsetsListener。
  • 已经试过了,不行
  • 实际上,当我尝试在没有 View. 部分的情况下实现 OnApplyWindowInsetsListener 时出现错误。
  • 您是否尝试过调试? public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) 被触发了吗?
  • 是的,我在调试,从不调用 public WindowInsets onApplyWindowInsets(View v, WindowInsets insets)。

标签: android wear-os


【解决方案1】:

API 23 介绍:

isScreenRound()

【讨论】:

  • 如果此答案提供更多上下文会很好。参考链接很好,但对于初学者(比如我自己)来说,很难看出如何在代码中使用它。
【解决方案2】:

非官方方式(不使用 WatchViewStub)——但对我来说这要容易得多。 https://github.com/tajchert/ShapeWear

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

【讨论】:

    【解决方案3】:

    使用 WatchViewStub 来膨胀视图。稍后在布局文件中,您可以为每个手表类型指定布局,如下所示。它将自动根据设备的需要选择正确的布局。

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      • 你能告诉我如何在我的代码中实现这个来获取磨损类型吗?
      • 我认为没有必要手动调用requestApplyInsets()。在没有它的情况下,我已经成功地调用了这个监听器。
      • @MaciejCiemięga 在 WatchViewStub 上,还是在其他 View 子类上?
      • @matiash:在任何视图上 - 例如FrameLayout、TextView 等
      • @MaciejCiemięga 我没有在其他视图上测试它,但WatchViewStub 确实手动调用了requestApplyInsets()。我的理论是它在布局时触发一次,因此如果您稍后添加侦听器,除非您再次请求,否则不会再次调用它。
      猜你喜欢
      • 2014-08-29
      • 2015-04-29
      • 2017-01-23
      • 1970-01-01
      • 2016-06-01
      • 2017-07-01
      • 1970-01-01
      • 2017-05-24
      • 1970-01-01
      相关资源
      最近更新 更多