簡體   English   中英

使用 textswitcher 切換文本時刪除邊距

[英]Remove margin while switching text using textswitcher

我正在使用 TextSwitcher 為文本更改設置動畫。 更改文本時,我正在使用向上滑動 animation 。 但是,文本不會向上滑動到容器。 看看下面發布的視頻。 文本既沒有從紅色邊框滑入,也沒有滑出到紅色順序。 我應該如何 go 實現文本從紅色邊框滑入並滑出到紅色邊框的行為?

在此處輸入圖像描述

布局如下所示:

<?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="wrap_content"
    android:layout_gravity="top">

    <androidx.cardview.widget.CardView
        android:id="@+id/iv_background"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        app:cardCornerRadius="20dp"
        app:cardElevation="0dp"
        app:cardPreventCornerOverlap="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.imageview.ShapeableImageView
            android:id="@+id/iv_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            app:shapeAppearance="@style/ContainerShape" />

        <View
            android:id="@+id/border"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/border" />

    </androidx.cardview.widget.CardView>

    <TextSwitcher
        android:id="@+id/tv_textswitcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:inAnimation="@anim/slide_up_from_bottom"
        android:outAnimation="@anim/slide_up_to_top"
        android:visibility="visible"
        app:layout_constrainedWidth="true"
        app:layout_constraintBottom_toBottomOf="@+id/iv_background"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/iv_background"
        app:layout_constraintTop_toTopOf="@+id/iv_background"
        app:layout_constraintVertical_bias="1.0">

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/text_background"
            android:ellipsize="end"
            android:lineHeight="20dp"
            android:text="Text 1"
            android:paddingStart="12dp"
            android:paddingTop="8dp"
            android:paddingEnd="12dp"
            android:paddingBottom="8dp"
            android:textSize="14sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/text_background"
            android:ellipsize="end"
            android:lineHeight="20dp"
            android:paddingStart="12dp"
            android:paddingTop="8dp"
            android:paddingEnd="12dp"
            android:paddingBottom="8dp"
            android:textSize="14sp" />

    </TextSwitcher>

</androidx.constraintlayout.widget.ConstraintLayout>

Animation 文件:

slide_up_from_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="1000"
        android:fromYDelta="100%"
        android:toYDelta="0" />
</set>

slide_up_to_top.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:duration="1000"
        android:fromYDelta="0"
        android:toYDelta="-100%" />
</set>

text_background.xml

<shape android:shape="rectangle">
            <corners android:radius="18dp" />
            <solid android:color="#e8e8e8" />
        </shape>

盡管 CardView 和 TextSwitcher 被限制在完全相同的位置,但它們的大小不同。 由於您設置的邊距,文本切換器的高度(和寬度)更小。

解決方案是從 TextSwitcher 中刪除邊距並將其設置為 TextViews。 如果這覆蓋了紅色邊框,您應該將 TextSwitcher 的背景設置為透明的。

實際上有多個問題需要在這里解決。

  1. 從 TextSwitcher 中刪除邊距並將其設置為 TextViews。 正如@D.McDermott 所提到的。
<TextSwitcher
        android:id="@+id/tv_textswitcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:inAnimation="@anim/slide_up_from_bottom"
        android:outAnimation="@anim/slide_up_to_top"
        android:visibility="visible"
        app:layout_constrainedWidth="true"
        app:layout_constraintBottom_toBottomOf="@+id/iv_background"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/iv_background"
        app:layout_constraintTop_toTopOf="@+id/iv_background"
        app:layout_constraintVertical_bias="1.0">

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:background="@drawable/text_background"
            android:ellipsize="end"
            android:lineHeight="20dp"
            android:paddingStart="12dp"
            android:paddingTop="8dp"
            android:paddingEnd="12dp"
            android:paddingBottom="8dp"
            android:text="Text 1"
            android:textSize="14sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:background="@drawable/text_background"
            android:ellipsize="end"
            android:lineHeight="20dp"
            android:paddingStart="12dp"
            android:paddingTop="8dp"
            android:paddingEnd="12dp"
            android:paddingBottom="8dp"
            android:textSize="14sp" />

    </TextSwitcher>
  1. animation 中的 yDelta 應該是相對於父級而不是相對於視圖。 所以,我們需要使用 100%p 而不是 100%。

slide_up_from_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="1000"
        android:fromYDelta="100%p"
        android:toYDelta="0" />
</set>

slide_up_to_top.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:duration="1000"
        android:fromYDelta="0"
        android:toYDelta="-100%p" />
</set>
  1. 將紅色邊框放在布局層次結構中 textview 上方。 這非常重要,因為這將確保 textview 消失在紅色邊框中,並且 go 不會超出它。 所以,我將邊框視圖移動到布局的底部。 理想情況下,您可以只將邊框提供給您的父母,而不用擔心放置此視圖。 但我有一些其他用例,所以我必須這樣做。
<View
        android:id="@+id/border"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/border"
        app:layout_constraintBottom_toBottomOf="@+id/iv_background"
        app:layout_constraintEnd_toEndOf="@+id/iv_background"
        app:layout_constraintStart_toStartOf="@+id/iv_background"
        app:layout_constraintTop_toTopOf="@+id/iv_background" />

完整的布局現在看起來像這樣:

<?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="wrap_content"
    android:layout_gravity="top">

    <androidx.cardview.widget.CardView
        android:id="@+id/iv_background"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        app:cardCornerRadius="20dp"
        app:cardElevation="0dp"
        app:cardPreventCornerOverlap="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.imageview.ShapeableImageView
            android:id="@+id/iv_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            app:shapeAppearance="@style/ContainerShape" />

    </androidx.cardview.widget.CardView>

    <TextSwitcher
        android:id="@+id/tv_textswitcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:inAnimation="@anim/slide_up_from_bottom"
        android:outAnimation="@anim/slide_up_to_top"
        android:visibility="visible"
        app:layout_constrainedWidth="true"
        app:layout_constraintBottom_toBottomOf="@+id/iv_background"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/iv_background"
        app:layout_constraintTop_toTopOf="@+id/iv_background"
        app:layout_constraintVertical_bias="1.0">

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:background="@drawable/text_background"
            android:ellipsize="end"
            android:lineHeight="20dp"
            android:paddingStart="12dp"
            android:paddingTop="8dp"
            android:paddingEnd="12dp"
            android:paddingBottom="8dp"
            android:text="Text 1"
            android:textSize="14sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:background="@drawable/text_background"
            android:ellipsize="end"
            android:lineHeight="20dp"
            android:paddingStart="12dp"
            android:paddingTop="8dp"
            android:paddingEnd="12dp"
            android:paddingBottom="8dp"
            android:textSize="14sp" />

    </TextSwitcher>

    <View
        android:id="@+id/border"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/border"
        app:layout_constraintBottom_toBottomOf="@+id/iv_background"
        app:layout_constraintEnd_toEndOf="@+id/iv_background"
        app:layout_constraintStart_toStartOf="@+id/iv_background"
        app:layout_constraintTop_toTopOf="@+id/iv_background" />

</androidx.constraintlayout.widget.ConstraintLayout>

這是生成的 animation 的 GIF:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM