gpt4 book ai didi

Android - 调用 GONE 然后 VISIBLE 使 View 显示在错误的位置

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:58:54 24 4
gpt4 key购买 nike

我有两个 View ,A 和 B, View A 在 View B 之上(都是线性布局)。

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

但是,当我再次将完全相同的 View (A) 设置为 VISIBLE 时,它显示在 View B 之上。我不希望这样。我希望 View B 回到其原始位置(在 View A 下方),这是我认为会发生的情况(但事实并非如此)。我该怎么做?

提前致谢!

编辑 - 代码

package com.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.ScaleAnimation;
import android.view.animation.Transformation;
import android.widget.LinearLayout.LayoutParams;

public class ViewGoneEffectActivity extends Activity implements OnClickListener {

private View viewComEfeito = null;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

findViewById(R.id.outroLinear).setOnClickListener(this);
findViewById(R.id.segundo).setOnClickListener(this);

viewComEfeito = findViewById(R.id.outroLinear);
}

@Override
public void onClick(View view) {

if (view.getId() == R.id.outroLinear) {

view.startAnimation(new MyScaler(1.0f, 1.0f, 1.0f, 0.0f, 500, view,
true));


}else if(view.getId() == R.id.segundo){
viewComEfeito.setVisibility(View.VISIBLE);
}
}

public class MyScaler extends ScaleAnimation {

private LayoutParams mLayoutParams;

private int mMarginBottomFromY, mMarginBottomToY;

private boolean mVanishAfter = false;

public MyScaler(float fromX, float toX, float fromY, float toY,
int duration, View view, boolean vanishAfter) {

super(fromX, toX, fromY, toY);
setDuration(duration);
mVanishAfter = vanishAfter;
mLayoutParams = (LayoutParams) view.getLayoutParams();

//int height = mView.getHeight();
int height = viewComEfeito.getHeight();
mMarginBottomFromY = (int) (height * fromY) + mLayoutParams.bottomMargin - height;
mMarginBottomToY = (int) (0 - ((height * toY) + mLayoutParams.bottomMargin)) - height;
}

@Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {

super.applyTransformation(interpolatedTime, t);

if (interpolatedTime < 1.0f) {
int newMarginBottom = mMarginBottomFromY + (int) ((mMarginBottomToY - mMarginBottomFromY) * interpolatedTime);
mLayoutParams.setMargins(mLayoutParams.leftMargin, mLayoutParams.topMargin, mLayoutParams.rightMargin, newMarginBottom);
viewComEfeito.getParent().requestLayout();

} else if (mVanishAfter) {
viewComEfeito.setVisibility(View.GONE);
}
}
}

这里是 XML:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
android:id="@+id/outroLinear"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to the real world" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Wow! =P" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Free your mind!" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="In Tylor we trust" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="First rule of fight club is" />

</LinearLayout>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:id="@+id/segundo" />


</LinearLayout>

最佳答案

您可以尝试将两个 View 放在 RelativeLayout 中并设置它们的相对位置。

关于Android - 调用 GONE 然后 VISIBLE 使 View 显示在错误的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7475053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com