随着Android Studio 2.2的预览1,谷歌在其支持库中发布了一个新的布局:ConstraintLayout。使用ConstraintLayout,在Android Studio中更容易使用设计工具,但我没有找到一种方法来使用相对大小(百分比或“权重”,如线性布局)。是否有一种基于百分比定义约束的方法?例如,让一个视图占屏幕的40%,在视图之间创建20%的空白,将一个视图的宽度设置为另一个视图宽度的50% ?


目前有几种方法可以做到这一点。

一种是创建导线(右键单击设计区域,然后单击添加垂直/水平导线)。然后,您可以单击指导方针的“标题”来更改定位为基于百分比。最后,您可以将视图约束为指导方针。

另一种方法是使用偏差(百分比)定位视图,然后将其他视图锚定在该视图上。

也就是说,我们一直在考虑如何提供基于百分比的维度。我不能做任何保证,但这是我们想补充的。

这里有一个快速的参考可能是有用的。

视图的放置

使用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"/>

然后你可以使用这个指导原则作为其他视图的锚点。

or

使用app:layout_constraintHorizontal_bias和/或app:layout_constraintVertical_bias在可用空间允许的情况下修改视图位置

<Button
    ...
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintHorizontal_bias="0.25"
    ...
    />

视图大小

另一个基于百分比的值是元素的高度和/或宽度,使用app:layout_constraintHeight_percent和/或app:layout_constraintWidth_percent:

<Button
    ...
    android:layout_width="0dp"
    app:layout_constraintWidth_percent="0.5"
    ...
    />

一个有用的补充是设置另一个维度相对于第一个维度,例如-根据宽度按比例设置高度:

  app:layout_constraintDimensionRatio="1:1"

指南是无价的- app:layout_constraintGuide_percent是一个很好的朋友…然而,有时我们想要没有指导方针的百分比。现在可以使用权重了:

android:layout_width="0dp"
app:layout_constraintHorizontal_weight="1"

下面是一个更完整的例子,它使用了附加权重的指导原则:

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

在“ConstraintLayout1.1.0-beta1”中,你可以使用百分比来定义宽度和高度。

android:layout_width="0dp"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent=".4"

这将定义宽度为屏幕宽度的40%。 这和百分比指导方针的组合允许您创建任何基于百分比的布局。

如何使用指南

公认的答案是有点不清楚如何使用指导方针和什么是“头”。

步骤

首先添加一个指南。

选择指导方针或移动它一点,使约束可见。

然后点击圆(“标题”),直到它变成一个百分比。然后你可以把这个百分比拖到50%或任何你想要的。

之后,你可以将你的视图约束到guideguide,使其成为父视图的一部分(在视图上使用match_constraint)。

你可以使用app:layout_constraintVertical_weight,它与linearlayout中的layout_weight相同

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

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

简单地,在指导标签中替换

app:layout_constraintGuide_begin="291dp"

with

app:layout_constraintGuide_percent="0.7"

其中0.7表示70%。

另外,如果您现在尝试拖动指导方针,拖动的值现在将以%age显示。

随着ConstraintLayout v1.1的新版本,你现在可以做以下事情:

<Button
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.2"
app:layout_constraintWidth_percent="0.65" />

这将限制按钮为父视图高度的20%和宽度的65%。

使用ConstraintLayout v1.1.2,维度应该设置为0dp,然后将layout_constraintWidth_percent或layout_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" />

(你不需要设置app:layout_constraintWidth_default="percent"或app:layout_constraintHeight_default="percent"与ConstraintLayout 1.1.2和以下版本)

试试这段代码。你可以用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>

Gradle:

dependencies {
...
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
}

我知道这不是OP最初要求的,但在这种情况下,当我有类似的问题时,这帮助了我很多。添加这为人们希望改变布局窗口大小(我经常使用),通过代码。将此添加到您的onCreate活动的问题。(改为80%)

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);

int width = dm.widthPixels;
int height = dm.heightPixels;

getWindow().setLayout((int)(width * 0.8), (int)(height * 0.8));

对于那些可能发现有用的人,你可以使用layout_constraintDimensionRatio在一个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"/>

在这种情况下,纵横比是16:9 app:layout_constraintDimensionRatio=“16:9”你可以在这里找到更多信息

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

<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"/>

你也可以用这种方式

android:layout_width="0dp"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.4"

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

这不是很棒吗?所有视图都支持layout_constraintWidth_percent和layout_constraintHeight_percent属性。这将导致约束固定在可用空间的一个百分比上。因此,让按钮或TextView扩展以填充屏幕的百分比可以用几行XML完成。

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

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_constraintWidth_percent="0.7" />

请注意,你将不得不把尺寸应该使用百分比到0dp,因为我们已经指定android:layout_width到0dp上面。

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

<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_constraintHeight_percent="0.2" />

看!我们已经指定android:layout_height为0dp这次,因为我们想按钮使用高度百分比。