在MotionLayout中水平对齐两个TextView [英] Horizontally Align Two TextViews in MotionLayout
问题描述
我正在尝试创建一个运动场景,其中两个文本视图以折叠的工具栏样式在向上拖动时从展开状态过渡到折叠状态.
I am trying to build a motion scene where two textviews are transitioned in a collapsing toolbar style from an expanded state to a collapsed state on dragging up.
两个textview分别在屏幕的左侧和右侧留有一定的边距,并且应彼此水平对齐.
The two textviews are positioned with some margins to the left and right side of the screen respectively and should be horizontally aligned to each other.
左侧的第一个textview与左侧的后退按钮箭头之间有一个空白,需要在父级中保持对齐.
The first textview on the left side has a margin from the back button arrow on its left and needs to be left aligned in parent.
右侧的第二个textview在其右侧和父级末尾之间有一个空白.
The second textview on the right side has a margin to its right and between the end of parent.
两个textview需要进行过渡,以使textsize可以平滑地翻译.
The two textviews needs to have a transition where the textsize smoothly translates.
我怎么能达到同样的目的?
How can I achieve the same?
动态布局:
<androidx.constraintlayout.motion.widget.MotionLayout
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"
app:layoutDescription="@xml/motion_scene">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="0dp"
android:layout_height="0dp"
android:clipToPadding="false"
android:paddingTop="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/space"/>
<View
android:id="@+id/space"
android:layout_width="0dp"
android:layout_height="110dp"
android:background="@color/white"
android:fitsSystemWindows="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:foreground="?attr/selectableItemBackground"
android:padding="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_view_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginEnd="10dp"
android:elevation="0dp"
android:textAlignment="viewStart"
android:textColor="@color/text_black"
android:text="text view 1"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="@id/space"
app:layout_constraintEnd_toStartOf="@id/text_view_2"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/text_view_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="20dp"
android:elevation="0dp"
android:textAlignment="viewEnd"
android:textColor="@color/text_black"
android:text="text view 2"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="@id/space"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/text_view_1" />
</androidx.constraintlayout.motion.widget.MotionLayout>
动态场景:
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Transition
app:constraintSetEnd="@id/state_collapsed"
app:constraintSetStart="@id/state_expanded">
<OnSwipe
app:dragDirection="dragUp"
app:touchAnchorId="@id/recyclerview"
app:touchAnchorSide="top" />
<KeyFrameSet>
<KeyAttribute
app:framePosition="50"
app:motionTarget="@id/text_view_1">
<CustomAttribute
app:attributeName="textSize"
app:customFloatValue="20" />
</KeyAttribute>
<KeyAttribute
app:framePosition="50"
app:motionTarget="@id/text_view_2">
<CustomAttribute
app:attributeName="textSize"
app:customFloatValue="20" />
</KeyAttribute>
</KeyFrameSet>
</Transition>
<ConstraintSet android:id="@+id/state_collapsed">
<Constraint android:id="@id/back">
<CustomAttribute
app:attributeName="elevation"
app:customDimension="6dp" />
</Constraint>
<Constraint
android:id="@id/space"
android:layout_height="?attr/actionBarSize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<CustomAttribute
app:attributeName="elevation"
app:customDimension="6dp" />
</Constraint>
<Constraint
android:id="@id/text_view_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:textAlignment="viewStart"
app:layout_constraintBottom_toBottomOf="@id/back"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/back">
<CustomAttribute
app:attributeName="textSize"
app:customFloatValue="16" />
<CustomAttribute
app:attributeName="elevation"
app:customDimension="6dp" />
</Constraint>
<Constraint
android:id="@id/text_view_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:textAlignment="viewEnd"
app:layout_constraintBottom_toBottomOf="@id/text_view_1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/text_view_1">
<CustomAttribute
app:attributeName="textSize"
app:customFloatValue="16" />
<CustomAttribute
app:attributeName="elevation"
app:customDimension="6dp" />
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/state_expanded">
<Constraint android:id="@id/back">
<CustomAttribute
app:attributeName="elevation"
app:customDimension="0dp" />
</Constraint>
<Constraint
android:id="@id/space"
android:layout_height="110dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<CustomAttribute
app:attributeName="elevation"
app:customDimension="0dp" />
</Constraint>
<Constraint
android:id="@id/text_view_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
app:layout_constraintBottom_toBottomOf="@id/space"
app:layout_constraintEnd_toStartOf="@id/text_view_1"
app:layout_constraintStart_toStartOf="parent">
<CustomAttribute
app:attributeName="textSize"
app:customFloatValue="24" />
<CustomAttribute
app:attributeName="elevation"
app:customDimension="0dp" />
</Constraint>
</ConstraintSet>
</MotionScene>
推荐答案
我在项目中有一个类似的目标,并且我使用了 android:scaleX
和 android:scaleY
属性在约束"中,因为使用CustomAttribute更改 textSize
attr并不顺利.当您使用 android:scaleX
和 android:scaleY
时,视图以中心为轴进行缩放.因此,您可能会遇到对齐问题.您必须设置 android:transformPivotX ="..."
和 android:transformPivotY ="..."
才能更改默认行为.结果,我遇到了这样的情况:
I had a similar goal in my project and I used android:scaleX
and android:scaleY
attributes in Constraints, because changing textSize
attr using CustomAttribute, didn't work smoothly. When you use android:scaleX
and android:scaleY
the view is scaled with a pivot in its center. So you may face alignment issues. You have to set android:transformPivotX="..."
and android:transformPivotY="..."
to change the default behavior. As a result, I had something like this in my case:
我要设置动画的TextView:
TextView I want to animate:
<TextView
android:id="@+id/title_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Text"
android:transformPivotX="0sp"
android:transformPivotY="24dp"/>
运动场景:
<?xml version="1.0" encoding="utf-8"?>
<MotionScene
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:constraintSetEnd="@id/collapsed"
motion:constraintSetStart="@id/expanded">
<OnSwipe
motion:dragDirection="dragUp"
motion:touchAnchorId="@+id/viewpager"
motion:touchAnchorSide="top" />
</Transition>
<ConstraintSet android:id="@+id/expanded">
<Constraint
android:id="@id/toolbar_image_iv"
android:layout_height="200dp"
...>
<CustomAttribute
motion:attributeName="imageAlpha"
motion:customIntegerValue="255" />
</Constraint>
<Constraint
android:id="@id/title_tv"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:scaleX="1.0"
android:scaleY="1.0"
...>
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/collapsed">
<Constraint
android:id="@id/toolbar_image_iv"
android:layout_height="?attr/actionBarSize"
...>
<CustomAttribute
motion:attributeName="imageAlpha"
motion:customIntegerValue="0" />
</Constraint>
<Constraint
android:id="@id/title_tv"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:scaleX="0.667"
android:scaleY="0.667"
...>
</Constraint>
</ConstraintSet>
</MotionScene>
这篇关于在MotionLayout中水平对齐两个TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!