Android – 调用GONE然后VISIBLE使视图显示在错误的位置

前端之家收集整理的这篇文章主要介绍了Android – 调用GONE然后VISIBLE使视图显示在错误的位置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个视图,A和B,视图A在视图B上方(都是线性布局).

当我以编程方式将视图A设置为GONE时,它将消失,并且它正下方的视图(B)将转到视图A的位置(如预期的那样).

但是,当我再次将相同的视图(A)设置为VISIBLE时,它会在视图B上显示.我不希望这样.我希望视图B回到原来的位置(在视图A下面),这是我认为会发生的事情(但事实并非如此).我怎样才能做到这一点?

先感谢您!

编辑 – 代码

  1. package com.test;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.view.animation.ScaleAnimation;
  8. import android.view.animation.Transformation;
  9. import android.widget.LinearLayout.LayoutParams;
  10.  
  11. public class ViewGoneEffectActivity extends Activity implements OnClickListener {
  12.  
  13. private View viewComEfeito = null;
  14.  
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17.  
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20.  
  21. findViewById(R.id.outroLinear).setOnClickListener(this);
  22. findViewById(R.id.segundo).setOnClickListener(this);
  23.  
  24. viewComEfeito = findViewById(R.id.outroLinear);
  25. }
  26.  
  27. @Override
  28. public void onClick(View view) {
  29.  
  30. if (view.getId() == R.id.outroLinear) {
  31.  
  32. view.startAnimation(new MyScaler(1.0f,1.0f,0.0f,500,view,true));
  33.  
  34.  
  35. }else if(view.getId() == R.id.segundo){
  36. viewComEfeito.setVisibility(View.VISIBLE);
  37. }
  38. }
  39.  
  40. public class MyScaler extends ScaleAnimation {
  41.  
  42. private LayoutParams mLayoutParams;
  43.  
  44. private int mMarginBottomFromY,mMarginBottomToY;
  45.  
  46. private boolean mVanishAfter = false;
  47.  
  48. public MyScaler(float fromX,float toX,float fromY,float toY,int duration,View view,boolean vanishAfter) {
  49.  
  50. super(fromX,toX,fromY,toY);
  51. setDuration(duration);
  52. mVanishAfter = vanishAfter;
  53. mLayoutParams = (LayoutParams) view.getLayoutParams();
  54.  
  55. //int height = mView.getHeight();
  56. int height = viewComEfeito.getHeight();
  57. mMarginBottomFromY = (int) (height * fromY) + mLayoutParams.bottomMargin - height;
  58. mMarginBottomToY = (int) (0 - ((height * toY) + mLayoutParams.bottomMargin)) - height;
  59. }
  60.  
  61. @Override
  62. protected void applyTransformation(float interpolatedTime,Transformation t) {
  63.  
  64. super.applyTransformation(interpolatedTime,t);
  65.  
  66. if (interpolatedTime < 1.0f) {
  67. int newMarginBottom = mMarginBottomFromY + (int) ((mMarginBottomToY - mMarginBottomFromY) * interpolatedTime);
  68. mLayoutParams.setMargins(mLayoutParams.leftMargin,mLayoutParams.topMargin,mLayoutParams.rightMargin,newMarginBottom);
  69. viewComEfeito.getParent().requestLayout();
  70.  
  71. } else if (mVanishAfter) {
  72. viewComEfeito.setVisibility(View.GONE);
  73. }
  74. }
  75. }

}

这里是XML:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent">
  7.  
  8. <LinearLayout
  9. android:id="@+id/outroLinear"
  10. android:orientation="vertical"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content">
  13.  
  14. <TextView
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:text="Welcome to the real world" />
  18.  
  19. <TextView
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:text="No" />
  23.  
  24. <TextView
  25. android:layout_width="fill_parent"
  26. android:layout_height="wrap_content"
  27. android:text="Wow! =P" />
  28.  
  29. <TextView
  30. android:layout_width="fill_parent"
  31. android:layout_height="wrap_content"
  32. android:text="Free your mind!" />
  33.  
  34. <TextView
  35. android:layout_width="fill_parent"
  36. android:layout_height="wrap_content"
  37. android:text="In Tylor we trust" />
  38.  
  39. <TextView
  40. android:layout_width="fill_parent"
  41. android:layout_height="wrap_content"
  42. android:text="First rule of fight club is" />
  43.  
  44. </LinearLayout>
  45.  
  46. <TextView
  47. android:layout_width="fill_parent"
  48. android:layout_height="wrap_content"
  49. android:text="@string/hello"
  50. android:id="@+id/segundo" />
  51.  
  52.  
  53. </LinearLayout>

解决方法

您可以尝试将两个视图放在RelativeLayout中并相对于彼此设置它们的位置.

猜你在找的Android相关文章