Animate LayoutChanges无法正常工作 [英] Animate LayoutChanges does not work properly
问题描述
我想在文本切换器中为文本设置动画。我遍历了所有堆栈问题和文档,但是它们并没有解决我的问题。
I want to animate text in my textswitcher. I went through all the stack questions and the documentation, but they didn't solve my problem.
这是我的XML:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv__inc_pre_sing__screen_title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#fff"
android:gravity="center"
android:text="PRACTICE"
android:textAllCaps="true"
android:textColor="@color/colorAccent"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.ConstraintLayout
android:id="@+id/rl__inc_pre_sing__tm_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#efff"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv__inc_pre_sing__screen_title">
<ImageView
android:id="@+id/tv__inc_pre_sing__quotation_mark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:src="@drawable/ic_archive_black_24dp"
app:layout_constraintTop_toTopOf="parent" />
<TextSwitcher
android:id="@+id/tv__inc_pre_sing__teacher_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/tv__inc_pre_sing__quotation_mark"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:src="@drawable/ic_archive_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv__inc_pre_sing__teacher_message" />
</android.support.constraint.ConstraintLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Click Me"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<View
android:layout_width="0dp"
android:layout_height="16dp"
android:background="@drawable/ic_launcher_background"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/rl__inc_pre_sing__tm_container" />
</android.support.constraint.ConstraintLayout>
这是我的代码
public class Main3Activity extends AppCompatActivity {
private TextSwitcher mSwitcher;
Button btnNext;
int currentIndex=-1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Resources resources = getApplicationContext().getResources();
final String[] textString = resources.getStringArray(R.array.teacher_messages);
btnNext = findViewById(R.id.button);
mSwitcher = findViewById(R.id.tv__inc_pre_sing__teacher_message);
mSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
public View makeView() {
// TODO Auto-generated method stub
// create new textView and set the properties like clolr, size etc
TextView myText = new TextView(Main3Activity.this);
myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
myText.setTextColor(Color.BLUE);
return myText;
}
});
// Declare the in and out animations and initialize them
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);
// set the animation type of textSwitcher
mSwitcher.setInAnimation(in);
mSwitcher.setOutAnimation(out);
// ClickListener for NEXT button
// When clicked on Button TextSwitcher will switch between texts
// The current Text will go OUT and next text will come in with specified animation
btnNext.setOnClickListener(new View.OnClickListener() {
//
public void onClick(View v) {
// TODO Auto-generated method stub
currentIndex++;
// If index reaches maximum reset it
if(currentIndex==textString.length)
currentIndex=0;
mSwitcher.setText(textString[currentIndex]);
((ViewGroup) findViewById(R.id.rl__inc_pre_sing__tm_container)).getLayoutTransition()
.enableTransitionType(LayoutTransition.CHANGING);
}
});
}
}
ConstraintLayout上id为的动画 rl__inc_pre_sing__tm_container
无法正常工作。假设我的TextSwitcher的文本长度大于3,然后TextSwitcher显示的文本长度为1,那么ConstraintLayout不会进行动画处理,如果下一个文本的长度为1,则约束布局会设置动画。
The animation on ConstraintLayout whose id is rl__inc_pre_sing__tm_container
does not work properly. Suppose my TextSwitcher has text whose length is greater than 3 and then TextSwitcher shows text whose length is 1 then the ConstraintLayout does not animate, if the next text is of length 1 then the constraintlayout animates.
我无法弄清楚这种奇怪的行为。
I am not able to figure out this weird behaviour.
推荐答案
这是问题所在:当 TextSwitcher
从X行文本过渡到X行以下,然后 TextSwitcher的
高度不变。
Here is the issue: When the TextSwitcher
transitions from X lines of text to something less than X lines then the TextSwitcher's
height does not change.
TextSwitcher
继承了 ViewAnimator
的某些行为,该行为考虑了所有视图的高度确定小部件的高度时。解决方案是将以下XML添加到布局文件中的 TextSwitcher
定义中,以禁止这种行为:
The TextSwitcher
inherits some behavior from ViewAnimator
which considers the height of all views when determining the height of the widget. The solution is to add the following XML to the TextSwitcher
definition in the layout file that will inhibit this behavior:
android:measureAllChildren="false"
感谢此堆栈溢出答案指出的所有这些都比我要好。
Thanks to this Stack Overflow answer that states all this better than I have.
这篇关于Animate LayoutChanges无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!