首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Android Wear上检测圆屏-设备上未调用onApplyWindowInsets

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

Stack Overflow用户
提问于 2014-08-26 05:58:32
回答 4查看 4.2K关注 0票数 2

我在一个Android Wear项目中实现了onApplyWindowInsets回调。在该方法中,它调用isRound()来确定代码是在圆形还是矩形Wear智能手表上运行。此回调是从圆形磨损仿真器调用的。(尽管isRound()返回false,但这是错误的。)但问题是,我的Samsung Gear Live从未调用过onApplyWindowInsets回调。因此,如果从onApplyWindowInsets放大视图,则视图不会被放大。有没有人让onApplyWindowInsets在真实的设备上运行?看起来这是不能保证的。如果是这样,那么如何根据屏幕类型放大不同的视图?

EN

回答 4

Stack Overflow用户

发布于 2014-08-30 00:43:19

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

代码语言:javascript
复制
 <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使用不同的it,您可以在onCreate上查看,如下所示:

代码语言:javascript
复制
@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不能同时为null和null。文档地址:https://developer.android.com/training/wearables/apps/layouts.html#UiLibrary

其他人尝试使用如下所示的使用setOnApplyWindowInsetsListener的策略:https://github.com/xamarin/monodroid-samples/blob/master/wear/GridViewPager/GridViewPager/MainActivity.cs。这对我不起作用。

票数 2
EN

Stack Overflow用户

发布于 2014-10-09 22:41:01

onApplyWindowInsets实际上要求开发人员在监听器中调用WatchViewStubonApplyWindowInsets方法。例如:

代码语言:javascript
复制
WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {

    @Override
    public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {
        //////////////////////////////////////////////////
        // IMPORTANT - the following line is required
        stub.onApplyWindowInsets(windowInsets);

        // Set the instance variable to say whether this is round...
        mIsRound = windowInsets.isRound();

        return windowInsets;
    }
});

虽然乍一看可能不直观,但这允许开发人员根据自己的选择覆盖默认的onApplyWindowInsets实现。

View.OnApplyWindowInsetsListener的文档声明:“侦听器可以选择性地调用参数视图的onApplyWindowInsets方法,以将视图的正常行为作为其自身行为的一部分。”

票数 2
EN

Stack Overflow用户

发布于 2014-09-02 05:55:28

谢谢你艾德!你的巧妙技巧似乎解决了这个问题。(我说“似乎”,因为只有在将mRoundBackground设置为非空值的真实圆形设备上测试代码后,我才能确定。)

现在有三个Android Wear形状(矩形/正方形,圆形/下巴和全圆形),我添加了两个全局值来查找屏幕形状,然后修改了回调,如下所示:

代码语言:javascript
复制
   stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            //mRectBackground = (RelativeLayout) findViewById(R.id.LinearLayout1);
            mRoundBackground = (RelativeLayout) findViewById(R.id.LinearLayout2);
            if (mRoundBackground != null) round = true;
            else round = false;

            WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
            Display display = window.getDefaultDisplay();
            //Added android.graphics.Point to imports for this next section
            Point point = new Point (0,0);
            display.getSize(point);
            scrnHeight = point.y;
            scrnWidth  = point.x;
            if (round && scrnWidth == scrnHeight) chin = false;
            else chin = true;
            //round = true; //For testing on the emulator!!!!!!
       }
    });

有没有人知道更好的检测Moto360下巴的方法?

更新:刚在Moto360上运行了这段代码,这项技术没有检测到圆屏。所以现在我只是检测高度是否等于宽度。但是,当更多的Android Wear设备问世时,这是一种糟糕的技术。因此,问题仍然存在,人们如何在真正的Android Wear设备上确定圆形屏幕?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25495005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档