ConstraintLayout:如何让视图成为屏幕宽度的一半并居中? [英] ConstraintLayout: how to have a view be half the screen width and centered?

查看:69
本文介绍了ConstraintLayout:如何让视图成为屏幕宽度的一半并居中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL;DR
视图宽度必须正好是屏幕的一半,并且居中.使用 ConstraintLayout.

请注意,视图没有任何内部宽度.

Note that the view does not have any inner width.

<View android:background="#ff0000" ... />

原始问题
我想实现一个布局,其中视图大小是屏幕大小的一半,并且水平居中.

Original question
I would like to achieve a layout where a view size is half the screen size, and centered horizontally.

像这样:|--view--|

Something like this: |--view--|

我找不到任何使用 ConstraintLayout 的方法.我发现的最好的方法是在分别位于完全左侧和完全右侧的 2 个假视图上使用 app:layout_constraintHorizo​​ntal_weight="1"app:layout_constraintHorizo​​ntal_weight="1.5"在我看来.

I can't find any way using ConstraintLayout. The best i found is by using app:layout_constraintHorizontal_weight="1" on 2 fake views positioned at the full left and full right respectively, and app:layout_constraintHorizontal_weight="1.5" on my view.

有什么更好的方法吗?

推荐答案

在测试版中,您可以使用百分比宽度.如果您无法使用 Beta 版,您可以使用两条垂直指南:一条位于屏幕宽度的 25%,另一条位于屏幕宽度的 75%.宽度为 0dp 的视图将被限制在这两个准则之间.此设置将为您提供一个屏幕宽度的 1/2 且居中的视图.

With the beta release you can use percentage widths. If you cannot use the beta release, you can employ two vertical guidelines: one at 25% of the screen width and one at 75% of the width. The view with a width of 0dp would be constrained between these two guidelines. This setup will give you a view that is 1/2 of the screen width and also centered.

以下 XML 演示了两种方式;一个使用 ConstraintLayout 测试版,第二个使用当前生产版本中可用的功能.

The following XML demonstrates both ways; one using the ConstraintLayout beta release and the second using features available in the current production release.

XML 布局

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main_inference"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <View
        android:id="@+id/viewTop"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginTop="16dp"
        android:background="@android:color/darker_gray"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_default="percent"
        app:layout_constraintWidth_percent="0.5" />


    <android.support.constraint.Guideline
        android:id="@+id/guidelineLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.25" />

    <View
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginTop="16dp"
        android:background="@android:color/darker_gray"
        app:layout_constraintEnd_toStartOf="@id/guidelineRight"
        app:layout_constraintStart_toEndOf="@id/guidelineLeft"
        app:layout_constraintTop_toBottomOf="@id/viewTop" />

    <android.support.constraint.Guideline
        android:id="@+id/guidelineRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.75" />

</android.support.constraint.ConstraintLayout>

这篇关于ConstraintLayout:如何让视图成为屏幕宽度的一半并居中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆