具有固定宽高比的ConstraintLayout链

时间:2018-05-31 14:18:12

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

我尝试根据以下布局构建包含3个imageView的活动:

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

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

  • 指导设置为父级高度的33%
  • A,B和C之间的水平链
  • B
  • 的固定纵横比
  • A,B和C使用match_constraint尺寸

虽然我希望求解器能够制作出所需的布局,但链似乎看起来与所有视图的宽度相似,如截图所示。

我也尝试使用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>

非常感谢!

2 个答案:

答案 0 :(得分:3)

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

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>

答案 1 :(得分:0)

我认为ConstraintLayout无法处理中间视图在高度或宽度,宽高比和边界方面的所有限制,因此该解决方案已部分实施在xml中并以编程方式进行最终调整。

它使用指南分隔三个图像,并在渲染布局时重新计算指南的位置。

布局

<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:id="@+id/mainLayout"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.Guideline
        android:id="@+id/guidelineV33"
        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/guidelineH33"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.3333" />

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

    <ImageView
        android:id="@+id/ivA"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:src="@drawable/ic_launcher_foreground"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/guidelineH33"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/guidelineV33"/>

    <ImageView
        android:id="@+id/ivB"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:src="@drawable/ic_launcher_foreground"
        app:layout_constraintLeft_toRightOf="@id/guidelineH33"
        app:layout_constraintRight_toLeftOf="@id/guidelineH66"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/guidelineV33"
         />
    <ImageView
        android:id="@+id/ivC"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:src="@drawable/ic_launcher_foreground"
        app:layout_constraintLeft_toRightOf="@id/guidelineH66"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/guidelineV33"/>


</android.support.constraint.ConstraintLayout>

活动

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.ConstraintSet;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ViewTreeObserver;

public class SO50626509Activity extends AppCompatActivity {

    private static final String TAG = SO50626509Activity.class.getName();

    ConstraintLayout cl;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.so50626509_layout);
        cl = findViewById(R.id.mainLayout);
        ViewTreeObserver observer = cl.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                adjustLayout();
                cl.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }


    private void adjustLayout(){
        int height = (int) (cl.getMeasuredHeight() * 0.3333);
        int width = cl.getMeasuredWidth();
        int middleWidth = height / 3;
        int g33 = (width - middleWidth) / 2;
        int g66 = g33 + middleWidth;


        ConstraintSet set = new ConstraintSet();
        set.clone(cl);
        set.setGuidelinePercent(R.id.guidelineH33,((float) g33 / (float) width));
        set.setGuidelinePercent(R.id.guidelineH66,((float) g66 / (float) width));
        set.applyTo(cl);
    }

}