android - 对齐圆形磨损设备角中的元素

标签 android android-layout wear-os

我正在尝试将一个对象对齐到圆形 android 穿戴设备的 round_layout 的一角。 正如您在这里看到的:

enter image description here

数字时钟将越界。

我在 2014 年谷歌 IO 上看过 android wear 文档,有人表示,有一行代码可以正确对齐对象。 我希望数字时钟位于左上角,但不要超出屏幕。

有人有什么建议吗?

这是 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>

编辑:

对于回合 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">

   <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 超出屏幕范围。我监督了什么?

最佳答案

根据 Google IO 2014 上显示的内容,您应该使用 BoxInsetLayout:
Check out this video <- 大约 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 ,然后所有内容都将保留在每一边的框中。当应用在方形 watch 上运行时,此布局的影响将被忽略。


编辑:

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

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

有 Activity :

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":(仅从顶部偏移)

enter image description here

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

enter image description here

关于android - 对齐圆形磨损设备角中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24724509/

相关文章:

Android Viewpager 性能

android - 错误 : Program type already present: com. google.common.annotations.Beta

android - 屏幕外的TextView

Android Wear 推荐字体

java - Android 在另一个弧内绘制一个弧

permissions - Android Wear 上请求权限

android - SQLiteOpenHelper onCreate()/onUpgrade() 何时运行?

android - ListView 布局返回空 View

android - 所有使用 hdpi xml 文件的设备

java - 即使我完成 Activity ,Firebase 连接仍保持 Activity 状态吗?