ConstraintLayout - 与自身成比例的宽度/高度?

dor*_*506 9 android android-constraintlayout

我试图找出如何使用约束布局实现以下行为:

  1. 将视图放在ConstraintLayout父级的中心
  2. 使视图宽度为父宽度的一半
  3. 使视图高度等于其宽度

(即 - 在中心放置一个正方形)

我试着用这个组合:

  android:layout_width="0dp"
  android:layout_height="0dp"
  app:layout_constraintBottom_toBottomOf="parent"
  app:layout_constraintEnd_toEndOf="parent"
  app:layout_constraintStart_toStartOf="parent"
  app:layout_constraintTop_toTopOf="parent"
  app:layout_constraintDimensionRatio="2:1"
Run Code Online (Sandbox Code Playgroud)

但我不确定如何从这里继续

rep*_*tch 10

为了使您孩子的宽度与父母的宽度相差一半,请使用指南:左边一个是0.25个百分点,右边一个是0.75

然后,将您的视图放在这些指南之间.

最后,将layout_constraintDimensionRatio设置为'1:1':

在此输入图像描述

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

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

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

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintLeft_toLeftOf="@id/guideline_left"
        app:layout_constraintRight_toRightOf="@id/guideline_right"
        app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)


ser*_*g3z 8

你可以在没有指南的情况下轻松完成.

只需使用app:layout_constraintWidth_percent ="0.5"

它适用于版本ConstraintLayout:1.1.0-beta5及更高版本.

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

    <ImageView
        android:id="@+id/img_icon"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/dark_red"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.5" />

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

在此输入图像描述


leo*_*ess 7

我不明白为什么你必须使用复杂的指南系统,而你也可以只使用:

app:layout_constraintWidth_percent="0.5"
Run Code Online (Sandbox Code Playgroud)

对于父级宽度的一半和

app:layout_constraintDimensionRatio="1: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">

    <View
        android:id="@+id/view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.5"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.5" />

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

  • 为`app:layout_constraintDimensionRatio="1:1"` 投票 (3认同)