使用WearableDrawerLayout时如何垂直居中元素?

时间:2017-02-09 16:14:48

标签: wear-os android-wear-2.0

我正在使用WearableDrawerLayout,并在带有下巴的模拟器上进行测试。我试图让一个元素垂直居中。相反,我看到的是元素集中在屏幕减去下巴的区域中。 - 即它有点向屏幕顶部移动。

我所看到的:

enter image description here

我应该看到的内容:

enter image description here

WearableDrawerLayout的(非公开?)来源我可以看出来,我认为这是由于这一点:

public WindowInsets onApplyWindowInsets(WindowInsets insets) {
    this.mSystemWindowInsetBottom = insets.getSystemWindowInsetBottom();
    if(this.mSystemWindowInsetBottom != 0) {
        MarginLayoutParams layoutParams = (MarginLayoutParams)this.getLayoutParams();
        layoutParams.bottomMargin = this.mSystemWindowInsetBottom;
        this.setLayoutParams(layoutParams);
    }

    return super.onApplyWindowInsets(insets);
}

如果没有这个问题我该怎么办?

编辑:这是另一个展示问题的布局示例:

enter image description here

如您所见,下巴未包含在可用区域中,这意味着BoxInsetLayout的高度应小于应有的高度。因此,它的按钮儿童太高了#34; - 他们没有底线。

这是我的编辑(抱歉我的Gimp技能),它显示了圆形显示,以及BoxInsetLayout和按钮的位置。

enter image description here

2 个答案:

答案 0 :(得分:3)

有几种方法可以实现这一目标。这是一个快速简单的...

首先,我用于测试的布局:

<android.support.wearable.view.BoxInsetLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/box">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/close_button"
            android:layout_centerInParent="true"
            android:background="#0000"/>
    </RelativeLayout>
</android.support.wearable.view.BoxInsetLayout>

这是基于BoxInsetLayout的最小示例,但原则应该扩展到更复杂的布局。我只是使用RelativeLayout来轻松地在屏幕中居中,drawable/close_button只是我坐在一个漂亮的圆形图形。

按原样,上述布局应位于任何方形或全圆形屏幕中:

fully round screen

为了将它置于“瘪胎”屏幕中,我们只需稍微调整根布局。这是我的Java代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    DisplayMetrics metrics = getResources().getDisplayMetrics();
    findViewById(R.id.box).getLayoutParams().height = metrics.widthPixels;
}

原始但有效:将height的{​​{1}}设置为等于屏幕宽度。然后布局将在该高度内居中。这是一个“瘪胎”屏幕:

flat tire screen

当然,您需要在布局的底部留出足够的空间,以确保您的内容不会被裁剪,但这对于“缺少”底部区域的屏幕来说是不可避免的。如果您使用BoxInsetLayout有任何元素,则可能需要手动补偿其位置,或者找到另一种方法来定位它们。

答案 1 :(得分:0)

您可以使用this post创建方形布局。

基本上,您将视图的左,右和上边缘约束到屏幕的边缘。然后,将视图的尺寸比限制为1:1。布局将满足您的左/右/顶部约束,然后尝试满足宽高比约束,并且唯一的移动方式是向下。所以,你有一个方形布局。

例如,

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <View
        android:id="@+id/your_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintDimensionRatio="1:1" />
</android.support.constraint.ConstraintLayout>