没有空间时将一个视图放在其他视图前面

问题描述 投票:0回答:1

当条形高度与其父级之间有空间时,我想在图形条顶部保留一个标签,但当图形条接触其父级顶部并且图形条之间没有空间时,希望在图形条前面显示标签顶部和父级。

将标签放置在图表栏上方的简单 xml 布局如下(用于测试 512dp 的随机高度):

<androidx.constraintlayout.widget.ConstraintLayout
....>
....
 <FrameLayout
    android:id="@+id/pragatiUchit"
    android:clickable="true"
    android:backgroundTint="?colorSecondaryVariant"
    android:background="@drawable/uchcha_vakriya"
    android:layout_width="match_parent"
    android:layout_height="512dp"
    app:layout_constraintBottom_toTopOf="@+id/tvDinaank"/>

<TextView
    android:id="@+id/tvUchit"
    android:text="100"
    android:background="#FF0000"
    android:textColor="?colorPrimary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="4dp"
    android:layout_marginBottom="4dp"
    app:layout_constraintBottom_toTopOf="@+id/pragatiUchit"
    app:layout_constraintStart_toStartOf="@+id/pragatiKul"
    app:layout_constraintEnd_toEndOf="@+id/pragatiKul"/>
....
</androidx.constraintlayout.widget.ConstraintLayout>

当图表栏达到全高度(match_parent)时,我想将标签放置在图表栏的前面,如下所示,就像标签的 top_to_top_of 父约束一样,而不是用其 Bottom_to_top_of 图表栏约束将其剪掉。

ConstraintLayout 中是否有任何方法可以实现这种动态行为或声明多个约束并指定它们的优先级?

android android-constraintlayout
1个回答
0
投票

没有办法在xml文件中制作它。你应该在你的代码中做到这一点。这是我的想法:

  • 首先,获取标签的高度(称为A)
  • 秒,检查图形栏和顶部屏幕之间的空间高度(称为B)。
  • 如果 A >= B,那么您可以使用 kotlin 或 java 更改标签的约束连接,如果 A < B, do it reverse

您可以在这里阅读更多答案 https://stackoverflow.com/a/45264822/17752564

© www.soinside.com 2019 - 2024. All rights reserved.