android - 具有固定纵横比的 ConstraintLayout 链

标签 android android-constraintlayout aspect-ratio constraint-layout-chains

我正在尝试根据以下布局使用 3 个 imageView 构建 Activity :

   <------W------->         <------W-------->
^  +---------------+--------+---------------+
|  |               |        |               |
|  |               |        |               |
H  |       A       |   B    |      C        |
|  |               | (1:3)  |               |
|  |               |        |               |
v  +---------------+--------+---------------+
  • H 应为 parent 高度的 1/3
  • B 的宽高比固定为 1:3
  • A和C的宽度应该相同

我尝试了多种解决方案,但始终找不到有效的解决方案。最后一个使用:

  • 准则设置为 parent 高度的 33%
  • A、B 和 C 之间的水平链
  • B 的固定纵横比
  • A、B 和 C 使用 match_constraint 维度

虽然我希望求解器会做出所需的布局,但链条似乎定义了与屏幕截图中所见的所有 View 相似的宽度。

我也尝试过使用 LinearLayout,但似乎无法固定其中项目的纵横比。

enter image description here

<?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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_bright"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintDimensionRatio=""
        app:layout_constraintEnd_toStartOf="@+id/imageView2"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@mipmap/ic_launcher" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_bright"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintDimensionRatio="1:3"
        app:layout_constraintEnd_toStartOf="@+id/imageView3"
        app:layout_constraintStart_toEndOf="@+id/imageView1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@mipmap/ic_launcher" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_bright"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/imageView2"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@mipmap/ic_launcher" />

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

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

</android.support.constraint.ConstraintLayout>

非常感谢!

最佳答案

可能有几种方法可以完成您的要求。这是一种方法:

enter image description here

此布局的关键是首先按照 XML 中的定义设置中心图像。一旦建立了中心图像,就可以更容易地定义左右图像。

<android.support.constraint.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageViewLeft"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="@+id/imageCenter"
        app:layout_constraintEnd_toStartOf="@+id/imageCenter"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/imageCenter"
        app:srcCompat="@color/colorPrimary" />

    <ImageView
        android:id="@+id/imageCenter"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="W,1:3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="0.33"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@color/colorAccent" />

    <ImageView
        android:id="@+id/imageViewRight"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="@+id/imageCenter"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/imageCenter"
        app:layout_constraintTop_toTopOf="@+id/imageCenter"
        app:srcCompat="@color/colorPrimary" />
</android.support.constraint.ConstraintLayout>

关于android - 具有固定纵横比的 ConstraintLayout 链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50626509/

相关文章:

android - 如何让 ConstraintLayout 中的 View 根据另一个 View 的可见性同时充当 wrap_content 和 match_parent

android - 如何将 ImageView 半重叠放置在 Fragment 中的 ConstraintLayout 顶部边界上?

android - 我如何制作一个左/右侧圆润如圆的按钮

css - 在不改变宽高比的情况下将图像的高度调整为 100% 宽度

android - 没有 Activity 的getContext

android - 如何在 phonegap 中使用插件来改变屏幕方向?

android - 在不使用webservice的情况下在android中连接外部数据库

android - 如何在android中使用 picasso 库将背景图像设置为 Activity 布局

java - 如何在android studio中 Intent 点击按钮从activity到fragment

android - 在 constraintView 中为 imageView 设置 minHeight 不起作用