如何最好地通过触摸来调整ConstraintLayout的大小?

考虑一个琐碎的 ConstraintLayout ,其中垂直线 GuideLine 占20%,在不可见线的左侧留有一个难看的垂直竖线(请参见XML)。

我可以按照tynn's example使用 ConstraintSet.setGuidelinePercent()以编程方式操作百分位数,但我需要更动态的实现(例如:可拖动的GuideLine或Barrier )。

必须比将拖动侦听器附加到垂直栏并使用 setGuidelinePercent()更好的方法?

ConstraintLayout 不会提供本机解决方案似乎令人难以理解-也许我只是找不到它?

示例XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <TextView
        android:id="@+id/leftTV"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="#AAA"
        android:text="LEFT: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/verticalBar"
        />

    <View android:id="@+id/verticalBar"
        android:layout_width="8dp"
        android:layout_height="match_parent"
        android:background="#000000"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toStartOf="@id/guideLine" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideLine"
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent=".20" />

    <TextView
        android:id="@+id/rightTV"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="#DDD"
        android:text="RIGHT: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@+id/guideLine"
        app:layout_constraintEnd_toEndOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>
wz15116339558 回答:如何最好地通过触摸来调整ConstraintLayout的大小?

看看MotionLayout。 (强调是我的。)

  

MotionLayout是一种布局类型,可帮助您管理应用程序中的运动和小部件动画。 MotionLayout是ConstraintLayout的子类,并基于其丰富的布局功能构建。作为ConstraintLayout库的一部分,MotionLayout可作为支持库使用,并且向后兼容API级别14。

     

除了描述布局之间的过渡之外,MotionLayout还允许您设置所有布局属性的动画。而且,它固有地支持可寻找的过渡。这意味着您可以根据某些条件(例如触摸输入)立即显示过渡中的任何点。

本文链接:https://www.f2er.com/3105531.html

大家都在问