【问题标题】:Android - Calling GONE and then VISIBLE makes the view to be shown in the wrong placeAndroid - 调用 GONE 然后 VISIBLE 会使视图显示在错误的位置
【发布时间】:2011-11-20 11:06:18
【问题描述】:

我有两个视图,A 和 B,视图 A 在视图 B 上方(​​都是线性布局)。

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

但是,当我再次将相同的视图 (A) 设置为 VISIBLE 时,它会显示在视图 B 上方。我不希望这样。我希望视图 B 回到其原始位置(在视图 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>

【问题讨论】:

  • 请添加一些代码让我们知道您的问题...
  • 我用代码编辑了我的问题。提前致谢!

标签: android user-interface view visibility


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    尝试将布局 A 和 B 放在另一个线性布局中,比如说 C,它具有以下属性:- height wrap_content , Orientation Vertical 它会按照你的意愿工作:)

    【讨论】:

      【解决方案3】:

      在视图变为可见后,您必须调用 measure(width,height)。 否则视图会以不同的方式显示。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多