setVisibility消失后,Android图片视图显示出来

时间:2015-12-01 21:41:28

标签: android imageview

我是android新手并尝试构建一个Android应用程序。

我将两个图像视图叠加在一起。我想要做的是当用户点击顶部图像时,底部图像显示出来。当用户点击底部图像时,顶部图像会显示出来。

问题是我单击顶部图像后,底部图像显示一秒钟,然后顶部图像再次显示。

以下是代码:

View view = inflater.inflate(R.layout.list_item, null);
ImageView top = (ImageView) view.findViewById(R.id.top);
top.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
               View parent = (View)v.getParent();
               ImageView top =            
                            (ImageView)parent.findViewById(R.id.top);
               top.setVisibility( View.GONE );

               ImageView bottom =    
                            (ImageView)parent.findViewById(R.id.bottom);
               bottom.setVisibility(View.VISIBLE);
        }
    });

   ImageView bottom = (ImageView) view.findViewById(R.id.bottom);
   bottom.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
              View parent = (View)v.getParent();
              ImageView top = 
                        (ImageView)parent.findViewById(R.id.top);
              top.setVisibility( View.VISIBLE );

              ImageView bottom =          
                        (ImageView)parent.findViewById(R.id.bottom);
              bottom.setVisibility(View.GONE);
       }
   });

这是布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
            android:id="@+id/bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_alignParentRight="true"
            android:src="@drawable/stop" />

        <ImageView
            android:id="@+id/top"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_alignParentRight="true"
            android:src="@drawable/ok" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView top = (ImageView) findViewById(R.id.top);
        top.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                View parent = (View) v.getParent();
                ImageView top = (ImageView) parent.findViewById(R.id.top);
                top.setVisibility(View.GONE);

                ImageView bottom = (ImageView) parent.findViewById(R.id.bottom);
                bottom.setVisibility(View.VISIBLE);
            }
        });

        ImageView bottom = (ImageView) findViewById(R.id.bottom);
        bottom.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                View parent = (View) v.getParent();
                ImageView top = (ImageView) parent.findViewById(R.id.top);
                top.setVisibility(View.VISIBLE);

                ImageView bottom = (ImageView) parent.findViewById(R.id.bottom);
                bottom.setVisibility(View.GONE);
            }
        });
    }  

}

我尝试了上面的代码及其工作,并没有改变图像。