BoxInsetLayout不起作用

时间:2015-07-03 14:13:34

标签: android android-layout wear-os

我在android studio v1.2.2中创建了示例android穿戴活动项目。我删除了WatchViewStub并使用this example创建了BoxInsetLayout的活动。但BoxInsetLayout在Round Device上无法正常工作。我在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?

2 个答案:

答案 0 :(得分:2)

错误发生在您指定的activity_main.xml中:

android:padding="15dp"

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

(编辑) 值得注意的是,BoxInsetLayout会调整两者本身的填充以及子视图。因此,请确保您不要更改填充值或事情会中断。如果您想要更多地控制填充,可以尝试嵌入第二个FrameLayout。

答案 1 :(得分:2)

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

Square screen Round Screen

我的

的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"

  

此行将填充分配给内部FrameLayout元素。这个   填充适用于方形和圆形屏幕。总填充量   按钮和窗口插件之间的平方屏幕上是20 dp   (15 + 5)和圆形屏幕上的5 dp。

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

Good Square screen Good Round screen

final 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>

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

相关问题