使用dimensionRatio子视图约束布局“wrap_content”

时间:2017-03-20 15:48:27

标签: android android-constraintlayout

在ConstraintLayout版本中< beta5,我的布局如下例所示:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

但是,从beta5开始的版本已删除子视图的MATCH_PARENT约束。

该文档提供了使用app的示例:layout_constraintDimensionRatio:

  

如果两个尺寸都设置为MATCH_CONSTRAINT,您也可以使用比率   (0dp)。在这种情况下,系统设置最大尺寸   满足所有约束并保持指定的宽高比。至   根据另一方的维度约束一个特定方。您   可以预先附加W,“或H,以约束宽度或高度   分别。例如,如果一个维度受两个约束   您可以指示目标(例如,宽度为0dp并以父项为中心)   应该约束哪一边,加上字母W(for   约束宽度)或H(用于约束高度)在前面   比率,用逗号分隔:

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

    <ImageView
        android:id="@+id/square_image"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintDimensionRatio="H, 1:1"/>

</android.support.constraint.ConstraintLayout>

https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html#DimensionConstraints

此示例在父布局具有固定高度或<Button android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintDimensionRatio="H,16:9" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent"/> 时有效,但在父布局设置为match_parent时则无效。

使用上面的代码示例,如果我将ImageView宽度设置为0dp,则父视图会折叠,就好像它没有内容一样。

这是一个非常有用的功能,我觉得我只是在这个新版本中遗漏了一些东西。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="https://use.fontawesome.com/b13050afbe.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.4.1/js/swiper.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.4.1/js/swiper.jquery.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.4.1/css/swiper.min.css" rel="stylesheet"/> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/> <div class="container-fluid"> <div class="content-wrapper narrow products-in-focus"> <div class="product-wrapper"> <div class="swiper-container products-in-focus"> <div class="swiper-wrapper"> <div class="swiper-slide"> <img src="http://www.placehold.it/80x130"> <div class="product-info-wrapper"> <div class="product-info"> <div class="row no-gutters"> <div class="col-7"> <strong class="text-uppercase">Amino Force</strong> <span class="product-info-link"><a href="#">Kurzinfo</a></span> </div> <div class="col-5 text-right"> <div class="product-info-price">CHF 34.00</div> </div> </div> </div> <div class="product-badge-wrapper"> <div class="product-badge dark-gray"> <div class="product-badge-content text-center"> %&nbsp;&nbsp; </div> </div> </div> </div> </div> <div class="swiper-slide"> <img src="http://www.placehold.it/80x130"> <div class="product-info-wrapper"> <div class="product-info"> <div class="row no-gutters"> <div class="col-8"> <strong class="text-uppercase">Basic Minerals</strong> <span class="product-info-link"><a href="#">Kurzinfo</a></span> </div> <div class="col-4 text-right"> <span class="product-info-price">CHF 34.00</span> </div> </div> </div> <div class="product-badge-wrapper"> <div class="product-badge red"> <div class="product-badge-content"> new </div> </div> </div> </div> </div> <div class="swiper-slide"> <img src="http://www.placehold.it/80x130"> <div class="product-info-wrapper"> <div class="product-info"> <div class="row no-gutters"> <div class="col-8"> <strong class="text-uppercase">Amino Force</strong> <span class="product-info-link"><a href="#">Kurzinfo</a></span> </div> <div class="col-4 text-right"> <span class="product-info-price">CHF 34.00</span> </div> </div> </div> </div> </div> <div class="swiper-slide"> <img src="http://www.placehold.it/80x130"> <div class="product-info-wrapper"> <div class="product-info"> <div class="row no-gutters"> <div class="col-8"> <strong class="text-uppercase">Whey Isolat CFM</strong> <span class="product-info-link"><a href="#">Kurzinfo</a></span> </div> <div class="col-4 text-right"> <span class="product-info-price">CHF 34.00</span> </div> </div> </div> <div class="product-badge-wrapper"> <div class="product-badge"> <div class="product-badge-content text-small"> nicht<br>auf lager </div> </div> </div> </div> </div> <div class="swiper-slide"> <img src="http://www.placehold.it/80x130"> <div class="product-info-wrapper"> <div class="product-info"> <div class="row no-gutters"> <div class="col-8"> <strong class="text-uppercase">Amino Force</strong> <span class="product-info-link"><a href="#">Kurzinfo</a></span> </div> <div class="col-4 text-right"> <span class="product-info-price">CHF 34.00</span> </div> </div> </div> <div class="product-badge-wrapper"> <div class="product-badge red"> <div class="product-badge-content"> new </div> </div> </div> </div> </div> <div class="swiper-slide"> <img src="http://www.placehold.it/80x130"> <div class="product-info-wrapper"> <div class="product-info"> <div class="row no-gutters"> <div class="col-8"> <strong class="text-uppercase">Basic Minerals</strong> <span class="product-info-link"><a href="#">Kurzinfo</a></span> </div> <div class="col-4 text-right"> <span class="product-info-price">CHF 34.00</span> </div> </div> </div> </div> </div> <div class="swiper-slide"> <img src="http://www.placehold.it/80x130"> <div class="product-info-wrapper"> <div class="product-info"> <div class="row no-gutters"> <div class="col-8"> <strong class="text-uppercase">Amino Force</strong> <span class="product-info-link"><a href="#">Kurzinfo</a></span> </div> <div class="col-4 text-right"> <span class="product-info-price">CHF 34.00</span> </div> </div> </div> </div> </div> <div class="swiper-slide"> <img src="http://www.placehold.it/80x130"> <div class="product-info-wrapper"> <div class="product-info"> <div class="row no-gutters"> <div class="col-8"> <strong class="text-uppercase">Whey Isolat CFM</strong> <span class="product-info-link"><a href="#">Kurzinfo</a></span> </div> <div class="col-4 text-right"> <span class="product-info-price">CHF 34.00</span> </div> </div> </div> <div class="product-badge-wrapper"> <div class="product-badge"> <div class="product-badge-content text-small"> nicht<br>auf lager </div> </div> </div> </div> </div> </div> </div> <div class="product-focus-prev"><i class="fa fa-chevron-left"></i></div> <div class="product-focus-next"><i class="fa fa-chevron-right"></i></div> </div> </div> </div>解决了这个问题。