【问题标题】:WatchViewStub round xml but loads square layoutWatchViewStub 圆形 xml 但加载方形布局
【发布时间】:2016-04-30 03:52:52
【问题描述】:

我是 android wear 开发的菜鸟,到目前为止一切都很好。 我已经成功创建了我的第一个 hello world 应用并在我的 LG G watch R 上运行它。

我遇到的问题如下; 我正在使用开发人员站点中描述的 WatchViewStub,WatchViewStub 应该检测应用程序正在运行的屏幕类型。 但显然 WatchViewStub 已经完成了它应该做的一半,这意味着应用程序使用正确的 xml 文件“round_activity”和正确的文本进行部署,但它仍然在设备上以方形布局显示。

据我了解,WatchViewStub 应该在检测到圆形显示时自动修复文本。但在我的情况下,它从正确的 round_activity xml 文件中获取正确的文本,但它以方形格式显示。 这是我的代码:

主活动:

public class MainActivity extends Activity {

    public TextView mTextView;

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

        WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
            @Override public void onLayoutInflated(WatchViewStub stub) {
                // Now you can access your views

                 mTextView = (TextView) stub.findViewById(R.id.text);

            }
        });
    }
}

activity_main.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_activity_main"
    app:roundLayout="@layout/round_activity_main"
    tools:context=".MainActivity"
    tools:deviceIds="wear">

    </android.support.wearable.view.WatchViewStub>

round_activity.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"
    android:orientation="vertical"
    tools:context=".MainActivity"
    tools:deviceIds="wear_square">

    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_round" />

</RelativeLayout>

square_activity.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"
        android:orientation="vertical"
        tools:context=".MainActivity"
        tools:deviceIds="wear_square">

        <TextView android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_square" />

    </RelativeLayout>

让我的 hello round world 文本正确显示的唯一方法是将其填充到 xml 的中心。

有人知道为什么会这样吗? 提前感谢各位大侠的帮助

【问题讨论】:

  • 两种布局看起来一样 - 目前唯一的区别是分配给您的 TextView 的字符串资源。

标签: android wear-os


【解决方案1】:

更改你的 round_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.BoxInsetLayout 
    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:layout_width="match_parent"
    android:layout_height="match_parent" >

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    tools:deviceIds="wear_square">

    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_round" />

</RelativeLayout>
</android.support.wearable.view.BoxInsetLayout>

【讨论】:

    【解决方案2】:

    在同一只手表上遇到同样的问题...通过将 Theme 更改为默认值来修复它。圆形/方形检测由主题完成。

    确保您在 Manifest 中的主题是: android:theme="@android:style/Theme.DeviceDefault"

    【讨论】:

      【解决方案3】:

      您可以使用可穿戴设备支持库中的BoxInsetLayout

      BoxInsetLayout 是一种可识别屏幕形状的 FrameLayout,它可以通过使用 layout_box 属性将其子项装箱在圆形屏幕的中心正方形中。

      【讨论】:

        猜你喜欢
        • 2015-04-29
        • 1970-01-01
        • 2019-02-11
        • 2011-09-24
        • 2013-08-06
        • 2011-10-13
        • 1970-01-01
        • 2016-02-11
        • 2020-05-25
        相关资源
        最近更新 更多