简体   繁体   English

Android ConstraintLayout垂直文本视图对齐

[英]Android constraintlayout vertical textview alignment

I have following layout: 我有以下布局:

<?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>

在此处输入图片说明

The problem is that green textview takes too much space and ignores top and bottom constraints. 问题在于绿色的textview占用太多空间,并且忽略了顶部和底部约束。 If I set green text view height to 0dp I get this: 如果我将绿色文本视图的高度设置为0dp,则会得到以下信息:

在此处输入图片说明

Which is almost what I want, but if I only have very little text I get: 这几乎是我想要的,但是如果我只有很少的文字,我会得到: 在此处输入图片说明

Here my red textview is left at the bottom, even though green text view has enough free space to shrink and let red textview go up. 即使绿色文本视图具有足够的可用空间来缩小并让红色textview向上,我的红色textview仍留在底部。

Basically I want my red view to always be bellow green view, but when red hits the bottom of parent window it should stop there and also stop green textview from expanding. 基本上,我希望我的红色视图始终是绿色的波纹管视图,但是当红色到达父窗口的底部时,它应该在那里停下来,并且也要阻止绿色textview扩展。

How do I achieve this? 我该如何实现?

Try this 尝试这个

(I set layout_width to match_parent..., you can change it if you want) (我将layout_width设置为match_parent ...,可以根据需要进行更改)

<?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>

While the accepted answer works, I noticed that the height of the bottom TextView will expand to fill the remaining vertical space of the container. 当接受的答案起作用时,我注意到底部TextView的高度将扩大以填充容器的剩余垂直空间。

With the help of a Guideline , we can give the bottom TextView a fixed height regardless if it has room to expand. 随着的帮助Guideline ,我们可以给底部TextView一个固定的高度,无论它是否有拓展空间。

dimens.xml dimens.xml

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

layout.xml layout.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>

How it works: 这个怎么运作:

The Guideline serves as the bottom constraint for the middle TextView , which is aligned to its top constraint via layout_constraintVertical_bias="0" . Guideline用作中间TextView的底部约束,该TextView约束通过layout_constraintVertical_bias="0"与其顶部约束对齐。

Setting layout_constrainedHeight="true" on the middle TextView ensures it never expands past the guideline. 在中间的TextView上设置layout_constrainedHeight="true"可以确保它永远不会超出准则。 This in turn ensures that the bottom TextView will never go off screen. 反过来,这可以确保底部的TextView永远不会离开屏幕。

在此处输入图片说明 在此处输入图片说明

you could try Linear Layout 你可以尝试线性布局

Edit New Code 编辑新代码

<?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" />

检查输出

我将删除TextView#2上的app:layout_constraintBottom_toTopOf="@+id/three"约束,不需要它,这可能是您看到第二个TextView占用多余空间的原因。

We should not use static value for height if content is dynamic.Don't align a view at bottom in constraint layout if you assign views in linear way 如果内容是动态的,我们不应该为高度使用静态值。如果您以线性方式分配视图,请不要在约束布局的底部对齐视图

 <?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM