如何制作视图" wrap_content但不大于"与ConstraintLayout?

时间:2017-05-21 17:44:49

标签: android android-constraintlayout

我连续3个视图:标题,版本和图像视图(作为按钮工作):

  1. 标题应为wrap_content,但尊重以下规则
  2. 版本应为wrap_content,位于标题右侧和imageview左侧
  3. imageview具有固定大小,位于父
  4. 的右上角

    enter image description here

    问题是如果标题太大,版本移动到右边并且规则"版本位于imageview"的左侧。不受尊重:

    enter image description here

    所以我需要限制标题宽度并使版本可见,而不是向右移动。

    这是XML:

    <?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:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:background="#b3b2b2">
    
        <!--  -->
        <TextView
            android:id="@+id/LibraryWithVersionItem.title"
            android:layout_width="0dp"
            android:textStyle="bold"
            android:textSize="@dimen/fontSize18"
            android:textColor="@color/mySecondaryDarkColor"
            android:layout_height="wrap_content"
            android:ellipsize="middle"
            tools:text="ExampleLibrary 01234567890123456789012345"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="8dp"
            app:layout_constraintLeft_toLeftOf="parent"
            />
    
        <Spinner
            android:id="@+id/LibraryWithVersionItem.versions"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:textSize="@dimen/fontSize16"
            android:textColor="@color/mySecondaryDarkColor"
            tools:listitem="@layout/library_version"
            android:layout_marginTop="@dimen/margin8"
            android:layout_marginLeft="@dimen/margin8"
            android:layout_marginRight="@dimen/margin8"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/LibraryWithVersionItem.title"
            app:layout_constraintRight_toLeftOf="@+id/LibraryWithVersionItem.info"
            app:layout_constraintHorizontal_bias="0.0"/>
    
        <TextView
            android:id="@+id/LibraryWithVersionItem.sentence"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@id/LibraryWithVersionItem.title"
            tools:text="Some library description in one sentence\nbut two lines"
            android:layout_marginTop="@dimen/margin8"
            android:layout_marginLeft="@dimen/margin8"
            app:layout_constraintRight_toLeftOf="@+id/LibraryWithVersionItem.install"
            android:layout_marginRight="8dp"
            app:layout_constraintHorizontal_bias="0.0"/>
    
        <TextView
            android:id="@+id/LibraryWithVersionItem.isInstalled"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/LibraryManager.installed"
            android:textColor="#1a7c1a"
            android:layout_marginTop="@dimen/margin8"
            android:layout_marginBottom="@dimen/margin8"
            android:layout_marginLeft="@dimen/margin8"
            android:layout_marginRight="@dimen/margin8"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@id/LibraryWithVersionItem.sentence"
            app:layout_constraintRight_toLeftOf="@+id/LibraryWithVersionItem.install"
            app:layout_constraintHorizontal_bias="0.0"/>
    
        <!-- information button -->
        <ImageView
            android:id="@+id/LibraryWithVersionItem.info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/margin8"
            android:paddingLeft="@dimen/margin8"
            android:paddingRight="@dimen/margin8"
            android:paddingBottom="@dimen/margin8"
            android:scaleType="center"
            android:src="@drawable/ic_info_outline_white_24dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    
        <!-- install button -->
        <ImageView
            android:id="@+id/LibraryWithVersionItem.install"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="@dimen/margin8"
            android:paddingRight="@dimen/margin8"
            android:paddingTop="@dimen/margin8"
            android:paddingBottom="@dimen/margin8"
            android:scaleType="center"
            android:src="@drawable/ic_get_app_white_24dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/LibraryWithVersionItem.info"/>
    
    
    </android.support.constraint.ConstraintLayout>
    

    PS 1. layout_width="0dp" + app:layout_constraintWidth_default="wrap"似乎正是我所需要的(&#34; wrap_content但不会破坏约束&#34;)但它不起作用(仍然大于要求) :

    <TextView
            android:id="@+id/LibraryWithVersionItem.title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:ellipsize="middle"
            android:textColor="@color/mySecondaryDarkColor"
            android:textSize="@dimen/fontSize18"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_default="wrap"
            tools:text="ExampleLibrary 01234567890123456789012345"
    

    PS 2.为版本(app:layout_constraintWidth_min="60dp")设置最小约束宽度也没有帮助 - 它是不可见的,因为它移动得太右了。

4 个答案:

答案 0 :(得分:18)

标题和版本应该在链中并使用app:layout_constraintWidth_default="wrap"

<?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:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:background="#b3b2b2">

    <!-- information button -->
    <ImageView
        android:id="@+id/LibraryWithVersionItem.info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/margin8"
        android:paddingLeft="@dimen/margin8"
        android:paddingRight="@dimen/margin8"
        android:paddingBottom="@dimen/margin8"
        android:scaleType="center"
        android:src="@drawable/ic_info_outline_white_24dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <!--  -->
    <TextView
        android:id="@+id/LibraryWithVersionItem.title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:ellipsize="middle"
        android:textColor="@color/mySecondaryDarkColor"
        android:textSize="@dimen/fontSize18"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_default="wrap"
        tools:text="ExampleLibrary 01234567890123456789012345"
        app:layout_constraintRight_toLeftOf="@+id/LibraryWithVersionItem.versions"
        android:layout_marginRight="8dp"
        android:layout_marginLeft="8dp"
        android:paddingBottom="1dp"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintHorizontal_bias="0.0"/>

    <Spinner
        android:id="@+id/LibraryWithVersionItem.versions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/fontSize16"
        android:textColor="@color/mySecondaryDarkColor"
        tools:listitem="@layout/library_version"
        app:layout_constraintRight_toLeftOf="@id/LibraryWithVersionItem.info"
        app:layout_constraintLeft_toRightOf="@+id/LibraryWithVersionItem.title"
        android:layout_marginRight="0dp"
        app:layout_constraintBottom_toBottomOf="@+id/LibraryWithVersionItem.title"
        android:layout_marginBottom="0dp"/>

    <TextView
        android:id="@+id/LibraryWithVersionItem.sentence"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/LibraryWithVersionItem.title"
        tools:text="Some library description in one sentence\nbut two lines"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="@dimen/margin8"
        app:layout_constraintRight_toLeftOf="@+id/LibraryWithVersionItem.install"
        android:layout_marginRight="8dp"
        app:layout_constraintHorizontal_bias="0.0"
        android:layout_marginStart="@dimen/margin8"
        android:layout_marginEnd="8dp"/>

    <TextView
        android:id="@+id/LibraryWithVersionItem.isInstalled"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/LibraryManager.installed"
        android:textColor="#1a7c1a"
        android:layout_marginTop="@dimen/margin8"
        android:layout_marginLeft="@dimen/margin8"
        android:layout_marginRight="@dimen/margin8"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/LibraryWithVersionItem.sentence"
        app:layout_constraintRight_toLeftOf="@+id/LibraryWithVersionItem.install"
        app:layout_constraintHorizontal_bias="0.0"
        android:layout_marginStart="@dimen/margin8"
        android:layout_marginEnd="@dimen/margin8"/>

    <!-- install button -->
    <ImageView
        android:id="@+id/LibraryWithVersionItem.install"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/margin8"
        android:paddingRight="@dimen/margin8"
        android:paddingTop="@dimen/margin8"
        android:paddingBottom="@dimen/margin8"
        android:scaleType="center"
        android:src="@drawable/ic_get_app_white_24dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/LibraryWithVersionItem.info"/>

</android.support.constraint.ConstraintLayout>

我尝试将版本与标题基线对齐,但如果标题是2行或更多行,则它与第一行对齐并且不需要。所以我必须将版本与标题底部和硬编码标题-3底部填充对齐。

enter image description here

enter image description here

但是,它在Android Studio中看起来很理想:

enter image description here

但在硬件设备上它看起来不同:enter image description here

Layout Inspector分析时,我可以看到标题宽度计算错误:enter image description here

可能是RecyclerView中使用它的副作用,但无论如何......

答案 1 :(得分:5)

你想设置android:layout_width =&#34; 0dp&#34;。

使用wrap_content,视图将随内容无限增长。通过使用0dp并设置其约束,视图将具有默认的最大大小,并且内容将在其内部增长达到限制。

使用android:layout_width =&#34; wrap_content&#34; Using android:layout_width="0dp"

使用android:layout_width =&#34; 0dp&#34; Using android:layout_width="wrap_content"

从这里开始,做你的魔法。你可以设置TextView的android:maxLines =&#34; 1&#34;和android:ellipsize =&#34;结束&#34;,在达到最大尺寸时添加三个点。 MaxLines="1" and Ellipsize="end"

最终布局xml

<div><iframe src="uploads/<?php echo $video['upload']; ?>"></div>

答案 2 :(得分:0)

我通过将layout_width设置为wrap_content而将maxWidth设置为值

来实现此目的

答案 3 :(得分:-1)

在ConstraintLayout中,您可以水平或垂直添加一个或多个指南,这有助于将屏幕分成几部分。 我使用* _pct属性添加指南,将位置设置为屏幕宽度/高度的百分比。