代码之家  ›  专栏  ›  技术社区  ›  SMGhost

Android ConstraintLayout垂直文本视图对齐

  •  0
  • SMGhost  · 技术社区  · 5 年前

    我有以下布局:

    <?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:id="@+id/dishContainer"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#0ff"
        android:clickable="true"
        android:focusable="true">
    
        <TextView
            android:id="@+id/one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:maxLines="3"
            android:background="#ff0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="One line lsfdjslkdf jksdljsdkljf sklksf klsjklsksjskfljsaklfj slkjfslkjfdskljfalskjflksdajfaklsjksadljfksfjksjkslfjsljsk lskjslksj ks flks jklsfjsl lsk slk" />
    
        <TextView
            android:id="@+id/two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#0f0"
            app:layout_constraintStart_toStartOf="@+id/one"
            app:layout_constraintTop_toBottomOf="@+id/one"
            app:layout_constraintBottom_toTopOf="@+id/three"
            tools:text="Two kksldfj lskfd lksd jlkfsd jksld ksldfj sdlks dklfklsffsd klsdfjklsdf kdsflkj skldfsf sdfksdjf jsdfsjklfklskfls fklsf jklsdfjksf lksjdflk sdklsf lksfdjkls fk" />
    
        <TextView
            android:id="@+id/three"
            android:layout_width="wrap_content"
            android:background="#f00"
            android:lines="1"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="@+id/two"
            app:layout_constraintTop_toBottomOf="@+id/two"
            app:layout_constraintBottom_toBottomOf="parent"
            tools:text="Third text that dissapears, but should not go below the parent" />
    
    </android.support.constraint.ConstraintLayout>
    

    enter image description here

    问题是,绿色textview占用了太多空间,忽略了顶部和底部约束。如果我将绿色文本视图高度设置为0dp 我明白了:

    enter image description here

    这几乎是我想要的,但如果我只有很少的文字,我会得到: enter image description here

    在这里,我的红色文本视图留在底部,即使绿色文本视图有足够的空间缩小并让红色文本视图上升。

    基本上,我希望我的红色视图总是低于绿色视图,但是当红色到达父窗口的底部时,它应该停止在那里,同时也阻止绿色文本视图的扩展。

    我怎样才能做到这一点?

    0 回复  |  直到 5 年前
        1
  •  1
  •   Ferran    5 年前

    试试这个

    (我将布局宽度设置为与父级匹配…,如果需要,可以更改它)

    <?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="vertical"
        android:paddingEnd="16dp"
        android:paddingStart="16dp"
        android:layout_width="match_parent"
        android:layout_height="100dp">
    
        <TextView
            android:id="@+id/one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="3"
            android:background="#ff0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="One line lsfdjslkdf jksdljsdkljf sklksf klsjklsksjskfljsaklfj slkjfslkjfdskljfalskjflksdajfaklsjksadljfksfjksjkslfjsljsk lskjslksj ks flks jklsfjsl lsk slk" />
    
        <TextView
            android:id="@+id/two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#0f0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/one"
            app:layout_constraintBottom_toTopOf="@id/three"
            app:layout_constrainedHeight="true"
            tools:text="Two kksldfj lskfd lksd jlkfsd jksld ksldfj sdlks dklfklsffsd klsdfjklsdf kdsflkj skldfsf sdfksdjf jsdfsjklfklskfls fklsf jklsdfjksf lksjdflk sdklsf lksfdjkls fk" />
    
        <TextView
            android:id="@+id/three"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="#f00"
            android:lines="1"
            app:layout_constraintHeight_min="20dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/two"
            app:layout_constraintBottom_toBottomOf="parent"
            tools:text="Third text that dissapears, but should not go below the parent" />
    
    </android.support.constraint.ConstraintLayout>
    
        2
  •  1
  •   user11566289user11566289    5 年前

    当接受的答案起作用时,我注意到底部的高度 TextView 将展开以填充容器的剩余垂直空间。

    借助于 Guideline ,我们可以给出 文本视图 一个固定的高度,不管它是否有空间膨胀。

    dimens.xml文件

    <dimen name="bottom_view_height">20dp</dimen>
    

    布局.xml

    <androidx.constraintlayout.widget.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">
    
        <TextView
            android:id="@+id/green"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#4CAF50"
            android:maxLines="3"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="@tools:sample/lorem/random"/>
    
        <TextView
            android:id="@+id/yellow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFEB3B"
            app:layout_constrainedHeight="true"
            app:layout_constraintTop_toBottomOf="@id/green"
            app:layout_constraintBottom_toTopOf="@id/bottom_guide"
            app:layout_constraintVertical_bias="0"
            tools:text="@tools:sample/lorem/random"
            tools:lines="100"/>
    
        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/bottom_guide"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_end="@dimen/bottom_view_height"/>
    
        <TextView
            android:id="@+id/red"
            android:layout_width="match_parent"
            android:layout_height="@dimen/bottom_view_height"
            android:background="#F44336"
            app:layout_constraintTop_toBottomOf="@id/yellow"
            tools:text="@tools:sample/lorem/random"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    工作原理

    这个 指南 作为中间层的底部约束 文本视图 ,它通过 layout_constraintVertical_bias="0" 是的。

    设置 layout_constrainedHeight="true" 在中间的textview确保它不会超出指导原则。这反过来又确保底部的文本视图永远不会离开屏幕。

    enter image description here enter image description here

        3
  •  0
  •   Mark    5 年前

    我会把 app:layout_constraintBottom_toTopOf="@+id/three" 对textview 2的限制,不需要它,这可能是您看到第二个textview占用额外空间的原因。

        4
  •  0
  •   Rajasekaran M    5 年前

    如果内容是动态的,则不应使用静态高度值。如果以线性方式分配视图,则不要在约束布局中对齐底部的视图

     <?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:id="@+id/dishContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#0ff"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:clickable="true"
        android:focusable="true">
    
        <TextView
            android:id="@+id/one"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:background="#ff0"
            android:maxLines="3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="One line lsfdjslkdf jksdljsdkljf sklksf klsjklsksjskfljsaklfj slkjfslkjfdskljfalskjflksdajfaklsjksadljfksfjksjkslfjsljsk lskjslksj ks flks jklsfjsl lsk slk" />
    
        <TextView
            android:id="@+id/two"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="#0f0"
            app:layout_constraintEnd_toEndOf="@+id/one"
            app:layout_constraintStart_toStartOf="@+id/one"
            app:layout_constraintTop_toBottomOf="@+id/one"
            tools:text="Two kksldfj lskfd lksd jlkfsd jksld ksldfj sdlks dklfklsffsd klsdfjklsdf kdsflkj skldfsf sdfksdjf jsdfsjklfklskfls fklsf jklsdfjksf lksjdflk sdklsf lksfdjkls fk" />
    
        <TextView
            android:id="@+id/three"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="#f00"
            android:lines="1"
            app:layout_constraintEnd_toEndOf="@+id/one"
            app:layout_constraintStart_toStartOf="@+id/one"
            app:layout_constraintTop_toBottomOf="@+id/two"
            tools:text="Third text that dissapears, but should not go below the parent" />
    
    </android.support.constraint.ConstraintLayout>
    
        5
  •  0
  •   Rizwan Ahmed Shivalli    5 年前

    你可以试试线性布局

    编辑新代码

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical">
    
    <TextView
        android:id="@+id/one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:maxLines="3"
        android:background="#ff0"
        android:text="One line lsfdjslkdf jksdljsdkljf sklksf klsjklsksjskfljsaklfj slkjfslkjfdskljfalskjflksdajfaklsjksadljfksfjksjkslfjsljsk lskjslksj ks flks jklsfjsl lsk slk"
        />
    
    <TextView
        android:id="@+id/two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:background="#0f0"
        android:text="Two kksldfj lskfd lksd jlkfsd jksld ksldfj sdlks dklfklsffsd klsdfjklsdf kdsflkj skldfsf sdfksdjf jsdfsjklfklskfls fklsf jklsdfjksf lksjdflk sdklsf lksfdjkls fk" />
    
    <TextView
        android:id="@+id/three"
        android:layout_width="match_parent"        
        android:background="#f00"        
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:text="Third text that dissapears, but should not go below the parent" />
    

    Check Output