【问题标题】:ConstraintLayout - proportional width/height to self?ConstraintLayout - 与自身成比例的宽度/高度?
【发布时间】:2018-04-03 21:14:05
【问题描述】:

我试图弄清楚如何使用约束布局来实现以下行为:

  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"

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

【问题讨论】:

    标签: android android-constraintlayout


    【解决方案1】:

    你可以不用指南,这很容易。

    只需使用 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>
    

    【讨论】:

    • 感谢您的回答。但是,当我将其复制并传递到 xml 文本编辑器时,预览显示正方形采用全宽,而不是半宽。我不明白为什么。它只发生在我身上吗?
    • @dor506 ConstraintLayout 版本有问题,我的版本implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta5'
    • 哦,我明白了。我正在使用 1.0.2。谢谢!
    【解决方案2】:

    要使您孩子的宽度为父母宽度的一半,请使用指南:左侧为 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>
    

    【讨论】:

    • 感谢您的精彩回答!你不认为使用 3 个元素来处理这样的小请求是多余的吗?我希望它只能通过视图元素和所需的属性来实现这一点
    • @dor506 是的,这是可能的。 stackoverflow.com/a/49648671/7825132
    【解决方案3】:

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

    app:layout_constraintWidth_percent="0.5"
    

    为父级的一半宽度和

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

    【讨论】:

    • app:layout_constraintDimensionRatio="1:1"点赞
    【解决方案4】:

    您可以使用指南 (Constraint to a guildeline) 实现此行为。您应该设置两个带有百分比的垂直指南(第一个 - 25% 和第二个 - 75%),这将为您提供父母宽度的一半。然后,您应该通过开始/结束将您的视图限制在这些指南中。此外,您应该通过顶部/底部将其限制为父级,并将视图的尺寸比设置为 1: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"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.constraint.Guideline
            android:id="@+id/guideline1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.25" />
    
        <android.support.constraint.Guideline
            android:id="@+id/guideline2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.75" />
    
        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintEnd_toEndOf="@id/guideline2"
            app:layout_constraintStart_toStartOf="@id/guideline1"
            app:layout_constraintTop_toTopOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 2016-11-20
      • 2015-06-09
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多