将元素对齐到圆形Wear设备的角落

7
我试图将一个对象对齐到我的圆形布局的角落,以适应圆形的Android Wear设备。
如下图所示: enter image description here 数字时钟会超出边界。
我看过Android Wear文档的Google IO 2014,有个人展示了一行代码,可以正确地对齐对象。我想让数字时钟位于左上角,但不要超出屏幕范围。
是否有人有建议?
以下是XML文件:
<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=".MyActivity"
tools:deviceIds="wear_round">

<DigitalClock
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/text"
    android:textSize="20sp"/>
</RelativeLayout>

编辑:

目前,我的xml布局文件对于圆形活动(round activity)看起来像这样:

<?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"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

   <TextView android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="1234567890"
       app:layout_box="top"/>

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

您可以看到,我尝试使用BoxInsetLayout,但输出仍然如下所示:
enter image description here
尽管我使用了
app:layout_box="top"

TextView超出了屏幕范围。我忽略了什么?

你现在只有这些吗?你尝试过使用边距、填充或动态移动视图吗? - zgc7009
我可以尝试添加填充或边距等,但我认为如果有另一种方法会更容易。我相信肯定有其他方法,只是想知道是否有人熟悉它。 - user2410644
@user2410644,请告诉我下面的解决方案是否适用于您:) 或者您在实施过程中有任何问题吗? - Maciej Ciemięga
@MaciejCiemięga 我已经编辑了我的帖子:对我来说似乎不起作用,我可能忽略了什么? - user2410644
@user2410644 我刚刚测试了这段代码,它可以正常工作。请查看我下面更新的答案。如果这段代码对你不起作用,那么很可能是你做错了什么。也许你正在使用“Android Wear Rect”系统和“AndroidWearRound”模拟器皮肤进行测试?请确保你在正确的模拟器配置上进行测试。 - Maciej Ciemięga
1个回答

13
根据 Google IO 2014 展示的内容,你应该使用 BoxInsetLayout:
查看这个视频 <- 大约在 6 分 04 秒处。
你可以在 DelayedConfirmation 示例代码中看到如何使用BoxInsetLayout。这是 main_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"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/grey"
        app:layout_box="top">

        [...]

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

您可以将 app:layout_box 设置为以下值:left|top|right|bottomall。在您的情况下,您可以使用 all,然后所有内容都将在每个方向上保持在框内。当应用在正方形手表上运行时,此布局的效果会被忽略。


编辑:

我刚测试了您发布的代码作为“不起作用”:

<?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"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

   <TextView android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="1234567890"
       app:layout_box="all"/>

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

有活动:

public class MainActivity extends Activity {

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

我已经告诉过你在 layout_box 中使用all 值,但是即使使用top,它也能正常工作:

app:layout_box="top":(仅从顶部偏移)

输入图像描述

app:layout_box="all":(从任何一侧偏移)

输入图像描述


"box"在属性"layout_box"中不是一个有效的值。请说明您的意思。 - Sam
你的xml代码没有问题。你是说你在IDE中遇到了错误?你解决了吗?关于模拟器的另一件事:也许你使用了带有AndroiWearRound皮肤的Android Wear Square模拟器? - Maciej Ciemięga
你必须使用 "all" 而不是 "box"。 - Sam
5
阅读此内容的其他人,请注意:为了正常工作,BoxInsetLayoutWatchViewStub 必须是您活动的视图的根!我吃了亏才发现这一点 :( - Sam
1
我撒谎了!我的问题实际上是由于我的活动主题引起的!只有少数主题适用于手表。(在sdk/platforms/android-20/data/res/values-watch/themes_device_defaults.xml下) - Sam
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接