我把我的问题重新描述成一个非常简单的表示。我有3个文本视图。其中2个位于单独的LinearLayout
中,第三个与LinearLayout
位于同一级别。
我正在切换test1
和test2
的可见性,我希望看到它们逐渐消失(这是有效的)。此外,我希望test3
能进入他的新位置(取代test1
和test2
)。我不能让这发生。test3
只是捕捉到它的新点。
我怎么能做到这一点?
我的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click" />
<LinearLayout android:animateLayoutChanges="true"
android:id="@+id/child1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/test1"
android:layout_width="match_parent" android:visibility="gone"
android:layout_height="wrap_content"
android:text="TEST1" />
<TextView
android:id="@+id/test2"
android:layout_width="match_parent" android:visibility="gone"
android:layout_height="wrap_content"
android:text="TEST2" />
</LinearLayout>
<TextView
android:id="@+id/test3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TEST3" />
</LinearLayout>
在我的活动中:
public class LayoutAnimations extends Activity {
private boolean toggle = true;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_animations);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (toggle) {
findViewById(R.id.test1).setVisibility(View.VISIBLE);
findViewById(R.id.test2).setVisibility(View.VISIBLE);
} else {
findViewById(R.id.test1).setVisibility(View.GONE);
findViewById(R.id.test2).setVisibility(View.GONE);
}
toggle = !toggle;
}
});
}
}
编辑:实际上,我在
TextView
和test1
旁边还有一个test2
应该随时可见,所以我不能只隐藏LinearLayout
本身。 最佳答案
我用另一个问题解决了。See this answer
引用:
我认为最简单的方法是扩展Animation
类并重写applyTransformation()
来更改视图的高度,如下所示:
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.LinearLayout;
public class MyCustomAnimation extends Animation {
public final static int COLLAPSE = 1;
public final static int EXPAND = 0;
private View mView;
private int mEndHeight;
private int mType;
private LinearLayout.LayoutParams mLayoutParams;
public MyCustomAnimation(View view, int duration, int type) {
setDuration(duration);
mView = view;
mEndHeight = mView.getHeight();
mLayoutParams = ((LinearLayout.LayoutParams) view.getLayoutParams());
mType = type;
if(mType == EXPAND) {
mLayoutParams.height = 0;
} else {
mLayoutParams.height = LayoutParams.WRAP_CONTENT;
}
view.setVisibility(View.VISIBLE);
}
public int getHeight(){
return mView.getHeight();
}
public void setHeight(int height){
mEndHeight = height;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
if (interpolatedTime < 1.0f) {
if(mType == EXPAND) {
mLayoutParams.height = (int)(mEndHeight * interpolatedTime);
} else {
mLayoutParams.height = (int) (mEndHeight * (1 - interpolatedTime));
}
mView.requestLayout();
} else {
if(mType == EXPAND) {
mLayoutParams.height = LayoutParams.WRAP_CONTENT;
mView.requestLayout();
}else{
mView.setVisibility(View.GONE);
}
}
}
}
要使用它,请按如下方式设置
onclick()
:int height;
@Override
public void onClick(View v) {
if(view2.getVisibility() == View.VISIBLE){
MyCustomAnimation a = new MyCustomAnimation(view2, 1000, MyAnimation.COLLAPSE);
height = a.getHeight();
view2.startAnimation(a);
}else{
MyCustomAnimation a = new MyCustomAnimation(view2, 1000, MyAnimation.EXPAND);
a.setHeight(height);
view2.startAnimation(a);
}
}
当做。