如何使ConstraintLayout使用百分比值?

Yur*_*rov 179 android android-support-library android-constraintlayout

通过Android Studio 2.2的预览1,Google在其支持库中发布了一个新的布局:ConstraintLayout.使用ConstraintLayout,在Android Studio中使用设计工具更容易,但我没有找到一种方法来使用相对大小(像LinearLayout中的百分比或'权重').有没有办法根据百分比定义约束?例如,使视图占据屏幕的40%,在视图之间创建20%的边距,将视图的宽度设置为另一个视图宽度的50%?

Rom*_*Guy 202

您目前可以通过几种方式执行此操作.一种是创建指南(右键单击设计区域,然后单击添加垂直/水平指南).然后,您可以单击指南的"标题",将定位更改为百分比.最后,您可以将视图限制为指南.另一种方法是使用偏差(百分比)定位视图,然后将其他视图锚定到该视图.

也就是说,我们一直在考虑如何提供基于百分比的维度.我不能做出任何承诺,但这是我们想要添加的东西.

  • 对于那些确切地想知道指南的标题是什么的人来说,它是具有向上或向下箭头或百分比符号的圆圈.单击该按钮在app:layout_constraintGuide_begin,app:layout_constraintGuide_end和app:layout_constraintGuide_percent之间切换.我遇到了一个小问题(在ConstraintLayout版本1.0.0-alpha8上,我必须单击指南,按住鼠标,然后拖动到圆圈以在编辑器中切换这些,但我确定这个错误很快就会修复. (19认同)
  • 您也可以使用`app:layout_constraintGuide_percentage`在布局的XML中进行设置。 (4认同)
  • @MatWilliams89有一个名为layout_constraintDimensionRatio的参数,我猜它可能对你的情况有用,在那里写"16:9".我没有成功尝试用它来构建你的布局,但是图像变得很大 (3认同)

Ami*_*val 159

这里有一个快速参考可能是有用的.
使用:这样的指南app:layout_constraintGuide_percent:

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.5"/>
Run Code Online (Sandbox Code Playgroud)

然后,您可以将此指南用作其他视图的锚点.

  • @user924:在 ConstraintLayout 中使用 `0dp` 来指示动态调整大小。请参阅 https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html#DimensionConstraints (3认同)
  • @ user924:你需要将它设置为宽度.`0dp`已被保留,因此`1dp`似乎是明智的. (3认同)
  • 为什么将宽度设置为1dp? (2认同)

hof*_*ord 105

从"ConstraintLayout1.1.0-beta1"开始,您可以使用百分比来定义宽度和高度.

android:layout_width="0dp"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent=".4"
Run Code Online (Sandbox Code Playgroud)

这将宽度定义为屏幕宽度的40%.百分比和此指南的组合允许您创建所需的任何基于百分比的布局.

  • 在约束布局1.0.2中,不支持**layout_constraintWidth_default**的**百分比**值.我认为正确的方法是使用指南. (6认同)
  • 如答案中所述,这是在ConstraintLayout的"1.1版"中添加的.在稳定版本上不再需要额外的`_default ="percent"`!请参阅https://developer.android.com/reference/android/support/constraint/ConstraintLayout上的**"百分比维度"** (4认同)

Ada*_*dam 71

使用ConstraintLayout v1.1的新版本,您现在可以执行以下操作:

<Button
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.2"
app:layout_constraintWidth_percent="0.65" />
Run Code Online (Sandbox Code Playgroud)

这会将按钮限制为屏幕高度的20%和屏幕宽度的65%.

  • 仅当我从“android:”更改为“app:”-&gt; app:layout_constraintHeight_percent="0.2" 时才起作用 (2认同)

Sur*_*gch 38

如何使用指南

接受的答案有一点不清楚如何使用指南以及"标题"是什么.

脚步

首先添加一个指南.

在此输入图像描述

选择Guidline或稍微移动它以使约束可见.

在此输入图像描述

然后单击圆圈("标题"),直到它变为百分比.然后,您可以将此百分比拖动到50%或任何您想要的.

在此输入图像描述

之后,您可以将视图限制为指南,使其占父级的一定百分比(match_constraint在视图上使用).

在此输入图像描述


Tom*_*rBu 31

指南是非常宝贵的 - 应用程序:layout_constraintGuide_percent是一个很好的朋友......但有时我们想要没有指导的百分比.现在可以使用权重:

android:layout_width="0dp"
app:layout_constraintHorizontal_weight="1"
Run Code Online (Sandbox Code Playgroud)

这是一个更完整的示例,它使用带有额外权重的指南:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context="android.itomerbu.layoutdemo.MainActivity">

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.44"/>

    <Button
        android:id="@+id/btnThird"
        android:layout_width="0dp"
        app:layout_constraintHorizontal_weight="1"
        android:layout_height="wrap_content"
        android:text="@string/btnThird"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintRight_toLeftOf="@+id/btnTwoThirds"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"/>

    <Button
        android:id="@+id/btnTwoThirds"
        app:layout_constraintHorizontal_weight="2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/btnTwoThirds"
        app:layout_constraintBottom_toBottomOf="@+id/btnThird"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/btnThird"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

  • @Plumbus-权重等于百分比(可以将任何百分比从数学上转换为权重,反之亦然)。由于对术语一无所知,您错过了答案的重点。 (2认同)

Ash*_*ute 21

约束布局 1.0 使视图占据屏幕的百分比需要制定两个准则。在约束布局 1.1 中,通过允许您轻松地将任何视图约束为百分比宽度或高度,它变得更加简单。

在此处输入图片说明

这不是很棒吗?所有视图都支持 layout_constraintWidth_percent 和 layout_constraintHeight_percent 属性。这些将导致约束固定为可用空间的百分比。因此,可以使用几行 XML 来使 Button 或 TextView 展开以填充屏幕的百分比。

例如,如果您想将按钮的宽度设置为屏幕的 70%,您可以这样做:

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_constraintWidth_percent="0.7" />
Run Code Online (Sandbox Code Playgroud)

请注意,您必须将尺寸应用作 0dp 的百分比,因为我们在上面已将 android:layout_width 指定为 0dp。

同样,如果你想把按钮的高度设置为屏幕的 20%,你可以这样做:

<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_constraintHeight_percent="0.2" />
Run Code Online (Sandbox Code Playgroud)

看!这次我们将 android:layout_height 指定为 0dp,因为我们希望按钮使用高度作为百分比。


Bub*_*ubu 11

使用ConstraintLayout v1.1.2时,应将维度设置为0dp,然后将layout_constraintWidth_percentlayout_constraintHeight_percent属性设置为0到1之间的值,如:

<!-- 50% width centered Button -->
<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintWidth_percent=".5" />
Run Code Online (Sandbox Code Playgroud)

(您无需设置app:layout_constraintWidth_default="percent"app:layout_constraintHeight_default="percent"使用ConstraintLayout 1.1.2及以下版本)


liv*_*ove 8

试试这个代码.您可以使用app:layout_constraintHeight_percent和app:layout_constraintWidth_percent更改高度和宽度百分比.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#FF00FF"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHeight_percent=".6"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent=".4"></LinearLayout>

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

摇篮:

dependencies {
...
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


vuh*_*990 6

你可以用app:layout_constraintVertical_weight同样的它layout_weightlinearlayout

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

<Button
    android:id="@+id/button4"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/button5"
    app:layout_constraintVertical_weight="1"/>

<Button
    android:id="@+id/button5"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintLeft_toRightOf="@+id/button4"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintVertical_weight="1"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

注:app:layout_constraintVertical_weight(app:layout_constraintHorizontal_weight)会与android:layout_width="0dp"(android:layout_height="0dp"


Jav*_*ier 6

对于可能觉得有用的人,您可以layout_constraintDimensionRatio在 a 内使用任何子视图ConstraintLayout,我们可以定义高度或宽度与其他尺寸的比率(至少一个宽度或高度必须为 0dp)示例

 <ImageView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:src="@drawable/top_image"
        app:layout_constraintDimensionRatio="16:9"        
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,纵横比为 16:9,app:layout_constraintDimensionRatio="16:9" 您可以在此处找到更多信息


Ash*_*ute 5

使用指南,您可以将定位更改为基于百分比

<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5"/>
Run Code Online (Sandbox Code Playgroud)

你也可以用这种方式

android:layout_width="0dp"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.4"
Run Code Online (Sandbox Code Playgroud)