指定移动和磨损模块的布局

Specifying layouts for mobile and wear modules

我从头开始为移动(minSdkVersion 9)和 wear(minSdkVersion 20)模块创建了一个新项目,我想知道如何指定 wear 的布局- round 和 wear-square(我需要玩一下 WatchViewStub class 吗?)设备。就像,我知道数据从手持设备发送到磨损,但我如何配置它以便应用程序相应地在磨损圆形和磨损方形布局上运行(特别是仅使用 XML 文件) ?

这是我的手机\...\activity_main.xml:

<?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=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Hello mobile world!"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

wear模块下的layout都是默认的(我没做任何修改),所以包括但不限于通过WatchViewStub实现的inflater class, XMLs 用于圆形和方形屏幕,应分别输出 "Hello Square World!" 和 "Hello Round World!",等等

这是我未动过的衣服\...\MainActivity:

package dpark.gameoflife;

import android.app.Activity;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView mTextView;

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

        final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
            @Override
            public void onLayoutInflated(WatchViewStub stub) {
                mTextView = (TextView) stub.findViewById(R.id.text);
            }
        });
    }
}

...然后这是我从 wear 虚拟模拟器中得到的:

为什么我上面的圆形虚拟模拟器不显示圆形?

谢谢,非常感谢。

资源限定符 layout-roundlayout-square 仅在 SDK 级别 23 中引入。要针对圆形和方形显示的不同布局的早期级别,您需要使用 WatchViewStubBoxInsetLayout,两者都是 documented here.

不过,根据您的发布计划,您也可以只针对可穿戴设备端的 SDK 23。 23 已经出现在大多数手表上,Google 表示所有 Wear 设备都将很快获得它。

至于你的圆形模拟器显示正方形......没有很好的解释,但他们有时就是这样做的。重新启动它几次,必要时删除并重新创建它,最终它应该会自行解决。