BoxInsetLayout 不起作用

BoxInsetLayout doesn't work

我在 android studio v1.2.2 中创建了示例 android wear activity 项目。我删除了 WatchViewStub 并使用 this example 和 BoxInsetLayout 创建了 activity。但是 BoxInsetLayout 在圆形设备上不能正常工作。我在 moto 360 android 5.1.1 和 moto 360 版本 5.0.1 更新之前测试了这个。我在模拟器上测试了这个。它根本不起作用。我每次都看到这个:

但必须是这个

我的代码如下:

activity_main.xml

<android.support.wearable.view.BoxInsetLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:padding="15dp">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp"
        android:background="@color/red"
        app:layout_box="all">

        <TextView
            android:gravity="center"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="Hello Android Wear"
            android:textColor="@color/black" />

        <ImageButton
            android:background="@drawable/selector_btn_ok"
            android:layout_gravity="bottom|start"
            android:layout_height="50dp"
            android:layout_width="50dp" />

        <ImageButton
            android:background="@drawable/selector_btn_cancel"
            android:layout_gravity="bottom|end"
            android:layout_height="50dp"
            android:layout_width="50dp" />
    </FrameLayout>
</android.support.wearable.view.BoxInsetLayout>

为什么不起作用?我哪里弄错了?以及如何在圆屏设备上正确使用 BoxInsetLayout?

错误在您的 activity_main.xml 中,您在其中指定:

android:padding="15dp"

如果你删除它,那么它应该可以工作。 BoxInsetLayout 在实现中使用了填充,并且您更改了给出您观察到的不正确输出的值。

(编辑) 重要的是要注意 BoxInsetLayout 调整自身和子视图中的填充。因此,请确保您没有更改填充值,否则会损坏。如果您想更好地控制填充,可以尝试嵌入第二个 FrameLayout。

我删除了 android:padding="15dp” 正如 所说,来自 BoxInsetLayout。现在 BoxInsetLayout 可以正常工作了,但是我遇到了一个新问题:FrameLayout 中的 android:padding="5dp" 不起作用。我尝试了 activity_layout 并添加了 id 并将 padding 更改为 20dp 我得到了圆形和方形屏幕的结果:

我的xml代码

<android.support.wearable.view.BoxInsetLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/first_frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/red"
        android:padding="20dp"
        app:layout_box="all">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Hello Android Wear"
            android:textColor="@color/black" />

        <ImageButton
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="bottom|start"
            android:background="@drawable/selector_btn_ok" />

        <ImageButton
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="bottom|end"
            android:background="@drawable/selector_btn_cancel" />
    </FrameLayout>
</android.support.wearable.view.BoxInsetLayout>

因此,填充在 id first_frame_layout 的 FrameLayout 中不起作用。 但是在官方文档中您可以在 FrameLayout

中阅读有关 android:padding="5dp"

This line assigns padding to the inner FrameLayout element. This padding applies to both square and round screens. The total padding between the buttons and the window insets is 20 dp on square screens (15+5) and 5 dp on round screens.

要快速解决此问题: 我添加了新的 FrameLayout,id 为 second_frame_layout 作为 FrameLayout 的子元素,id 为 first_frame_layout。我从 first_frame_layout 中删除了填充,并将 padding="5dp" 添加到 second_frame_layout。我得到了这个结果:

决赛activity_main.xml

<android.support.wearable.view.BoxInsetLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/first_frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/red"
        app:layout_box="all">

        <FrameLayout
            android:id="@+id/second_frame_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/green"
            android:padding="5dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Hello Android Wear"
                android:textColor="@color/black" />

            <ImageButton
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="bottom|start"
                android:background="@drawable/selector_btn_ok" />

            <ImageButton
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="bottom|end"
                android:background="@drawable/selector_btn_cancel" />
        </FrameLayout>
    </FrameLayout>
</android.support.wearable.view.BoxInsetLayout>

现在。它在方形和圆形屏幕上以我想要的方式工作。