有一个奇怪的。我设法把它归结为一个非常简单的例子。实在想不通。
我在 LinearLayout 中有一个 LinearLayout。我想使用动画调整 child 的大小(有时我正在努力)。这是我的布局 XML。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
<!-- Lots more controls will appear above -->
<LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:id="@+id/animation_subject">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:gravity="center" />
</LinearLayout>
<!-- Lots more controls will appear below -->
</LinearLayout>
<Button android:text="Slide it Down!" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="btnDoAnimDown_OnClick" />
</LinearLayout>
现在,如您所见,有一个按钮,下面是该按钮的代码。
public void btnDoAnimDown_OnClick(View v)
{
View pnlSlider = findViewById(R.id.animation_subject);
pnlSlider.measure(1000, 1000);
DropDownAnim anim = new DropDownAnim(pnlSlider, pnlSlider.getMeasuredHeight(), true);
anim.setDuration(2000);
pnlSlider.startAnimation(anim);
return;
}
如果您现在运行它,它不会滑落。完全没有。但是,如果您要将 Button 移动到我命名为 Overview 的 LinearLayout 中,并将其放在 Child LinearLayout 之后,效果会很好!像这样...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
<!-- Lots more controls will appear above -->
<LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:id="@+id/animation_subject">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:gravity="center" />
</LinearLayout>
<!-- Lots more controls will appear below -->
<Button android:text="Slide it Down!" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="btnDoAnimDown_OnClick" />
</LinearLayout>
</LinearLayout>
它现在完全按照预期向下滑动。很明显,父布局出了问题……因为 getMeasuredHeight() 返回了正确的值,只是动画没有真正运行!
这是动画课,我是不是错过了什么愚蠢的东西?可能是!
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class DropDownAnim extends Animation {
int targetHeight;
View view;
boolean down;
public DropDownAnim(View view, int targetHeight, boolean down) {
this.view = view;
this.targetHeight = targetHeight;
this.down = down;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newHeight;
if (down) {
newHeight = (int) (targetHeight * interpolatedTime);
} else {
newHeight = (int) (targetHeight * (1 - interpolatedTime));
}
Log.d("new height", String.valueOf(newHeight));
view.getLayoutParams().height = newHeight;
view.requestLayout();
}
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, ((View)view.getParent()).getWidth(), parentHeight);
}
@Override
public boolean willChangeBounds() {
return true;
}
}
任何帮助都会很棒!
最佳答案
我来猜猜看。
在您的第一个布局中,内部布局和按钮共享外部线性布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
<!-- Lots more controls will appear above -->
<LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:id="@+id/animation_subject">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:gravity="center" />
</LinearLayout>
<!-- Lots more controls will appear below -->
</LinearLayout>
<Button android:text="Slide it Down!" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="btnDoAnimDown_OnClick" />
</LinearLayout>
这使得外部布局成为 ViewGroup!
但是在 Animation 的初始化方法中,您对给定 View 的父级进行了转换。
您将它转换到 View,它不在您的第一个布局中。
在你的第二个,这就是它工作的原因。
所以我希望您尝试将其转换为 ViewGroup:
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, ((ViewGroup)view.getParent()).getWidth(), parentHeight);
}
关于android - 一旦高度为 0,LinearyLayout 将不会调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10549580/