【问题标题】:How to vertically center an element when using WearableDrawerLayout?使用 Wea​​rableDrawerLayout 时如何垂直居中元素?
【发布时间】:2017-02-09 16:14:48
【问题描述】:

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

我看到了什么:

我应该看到什么:

从我在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);
}

我该怎么做才能不出现这个问题?

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

如您所见,可用区域不包括下巴,这意味着BoxInsetLayout 的高度小于应有的高度。因此,它的子按钮太“高”——它们没有底部对齐。

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

【问题讨论】:

    标签: wear-os android-wear-2.0


    【解决方案1】:

    有几种方法可以做到这一点。这是一个快速简便的方法...

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

    <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 只是我坐在那里的一个漂亮的圆形图形。

    照原样,上述布局应该在任何方形或全圆形屏幕中居中:

    要使其在“爆胎”屏幕中居中,我们只需稍微调整根布局即可。这是我的 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;
    }
    

    粗略但有效:将BoxInsetLayoutheight 设置为等于屏幕的宽度。然后布局将在该高度内居中。这是在“爆胎”屏幕上:

    当然,您需要在布局的底部留出足够的空间,以免内容被裁剪,但如果屏幕底部“缺少”一个区域,这是不可避免的。如果您有任何使用android:layout_alignBottom 的元素,您可能需要手动补偿它们的位置,或者寻找其他方式来定位它们。

    【讨论】:

    • 这个技巧很有效,非常感谢!我仍然认为 WearableDrawerLayout 的下巴行为是不正确的——我不确定你是否同意(我想我会提出问题)。但我很高兴有一个解决方案:)
    【解决方案2】:

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

    基本上,您将视图的左、右和上边缘限制在屏幕边缘。然后,将视图的尺寸比限制为 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-31
      • 2014-04-05
      • 1970-01-01
      • 2023-01-18
      • 2011-03-20
      • 2020-11-13
      • 2013-12-06
      • 2011-04-20
      相关资源
      最近更新 更多