Android Wear Round Emulator扩大Square布局 [英] Android Wear Round Emulator inflating Square Layout

查看:113
本文介绍了Android Wear Round Emulator扩大Square布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先要说的是,我知道这里已经有类似的问题,但是没有一个人解决了这个问题

我在Ubuntu上使用Round Android Wear模拟器(Android 5.0.1和x86).屏幕将显示为圆形,并且模拟器上的所有其他功能(设置表盘,卡片)将像在圆形表上一样显示.但是,在我的应用程序中,我使用的是WatchViewStub,尽管我在布局中指定了以下内容,但圆形仿真器仍在扩大正方形布局:

I am using the Round Android Wear Emulator (Android 5.0.1 and x86) on Ubuntu. The screen appears round, and all other features on the emulator (set watch face, cards) appear as they would on a round watch. However in my app, I am using a WatchViewStub, and the round emulator is inflating the square layout despite me specifying the following in the layout:

app:rectLayout="@layout/rect_activity_setup"
app:roundLayout="@layout/round_activity_setup"

这是我的活动代码:

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

    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
      @Override
      public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {
        stub.onApplyWindowInsets(windowInsets);
        return windowInsets;
      }
    });
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
      @Override
      public void onLayoutInflated(WatchViewStub stub) {
        // ...
      }
    });
  }

还有什么需要告诉应用程序设备是圆形的吗?还是这是模拟器的错误?

Is there anything else I need to do to tell the app that the device is round? Or is this a bug with the emulator?

推荐答案

因此,您可以通过设置自定义视图来实现.假设您要为FrameLayout充气.您将有两个视图. RoundFrameLayout.java和RectFrameLayout.java.然后让它们都实现以下

So you do it by setting a custom view. Let's say you want to inflate a FrameLayout. You would have two views. RoundFrameLayout.java and RectFrameLayout.java. Then have them both implement the following

public interface RoundAware {
    boolean isRound();
}

RoundFrameLayout看起来像这样.

The RoundFrameLayout will look like this.

public class RoundFrameLayout extends FrameLayout implements RoundAware {
    public RoundFrameLayout(Context context) {
        super(context);
    }

    public RoundFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RoundFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public RoundFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean isRound() {
        return true;
    }
}

RectFrameLayout看起来像这样.

The RectFrameLayout will look like this.

public class RectFrameLayout extends FrameLayout implements RoundAware {
    public RoundFrameLayout(Context context) {
        super(context);
    }

    public RoundFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RoundFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public RoundFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean isRound() {
        return false;
    }
}

然后将视图存根的圆形和矩形布局正确指向RoundFrameLayout和RectFrameLayout xml文件.如果希望它们中包含相同的内容,请对视图的所有子级使用每个文件中的include标记.

Then point your round and rect layouts for the view stub correctly at the RoundFrameLayout and RectFrameLayout xml file. If you want them to have the same stuff in them use the includes tag in each file for all of the view's children.

这篇关于Android Wear Round Emulator扩大Square布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆