【问题标题】:ImageView showing previous image view's dataImageView 显示上一个图像视图的数据
【发布时间】:2013-03-16 08:25:07
【问题描述】:

我有以下看法:

如果我点击任何 ImageButton 以全屏模式显示相应的图像。例如,如果我点击 Black ,它会显示一个图像视图,例如:

我还为一些像这样的图像按钮将背景设置为全屏颜色:如果我点击粉红色,它会显示纯粉红色:

到目前为止一切正常,如果我先点击黑色按钮,然后点击粉色,黑色上的图像将继续以粉红色背景布局,如下所示:

这是我的问题,我只想要粉色,而不是前景中的图像;

这是我的 XML 文件:

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#657383" >

<ImageView android:id="@+id/imv"
        android:contentDescription="@string/app_name"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone"
        />

<Button android:id="@+id/home"
    android:contentDescription="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="10dp"
    android:text="@string/Back_Button"
    android:textSize="25sp"
    />


    <RelativeLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content">


    <ImageButton android:id="@+id/image1"
        android:contentDescription="@string/hello_world"
        android:layout_width="130dp"
        android:layout_height="95dp"
        android:background="#000000"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"/>

    <ImageButton android:id="@+id/image2"
        android:contentDescription="@string/hello_world"
        android:layout_width="130dp"
        android:layout_height="95dp"
        android:layout_toRightOf="@+id/image1"
        android:background="#ff0000"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"/>

    <ImageButton android:id="@+id/image3"
        android:contentDescription="@string/hello_world"
        android:layout_width="130dp"
        android:layout_height="95dp"
        android:layout_below="@+id/image1"
        android:background="#00ff00"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"
        />

    <ImageButton android:id="@+id/image4"
        android:contentDescription="@string/hello_world"
        android:layout_width="130dp"
        android:layout_height="95dp"
        android:background="#0000ff"
        android:layout_below="@+id/image2"
        android:layout_toRightOf="@+id/image3"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"
        />

    <ImageButton android:id="@+id/image5"
        android:contentDescription="@string/hello_world"
        android:layout_width="130dp"
        android:layout_height="95dp"
        android:background="#FFFF00"
        android:layout_below="@+id/image3"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"
        />

    <ImageButton android:id="@+id/image6"
        android:contentDescription="@string/hello_world"
        android:layout_width="130dp"
        android:layout_height="95dp"
        android:layout_below="@+id/image4"
        android:layout_toRightOf="@+id/image5"
        android:background="#FFA500"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"/>

    <ImageButton android:id="@+id/image7"
        android:contentDescription="@string/hello_world"
        android:layout_width="130dp"
        android:layout_height="95dp"
        android:layout_below="@+id/image5"
        android:background="#FFFFFF"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"/>

    <ImageButton android:id="@+id/image8"
        android:contentDescription="@string/hello_world"
        android:layout_width="130dp"
        android:layout_height="95dp"
        android:layout_below="@+id/image6"
        android:background="#F52887"
        android:layout_toRightOf="@+id/image7"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"/>



</RelativeLayout>

我的 .Java 数据是:

      private OnClickListener myListener = new OnClickListener() {

    @SuppressLint("NewApi")
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub


        try {

            handler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    iv.setVisibility(View.GONE);
                }
            }, 2500);



            if (v.getId() == R.id.image1) {
                iv.setBackground(null);
                iv.setVisibility(View.VISIBLE);
                iv.setImageResource(R.drawable.ic_launcher);

            } else if(v.getId() == R.id.image2){
                iv.setBackground(null);

                iv.setVisibility(View.VISIBLE);
                iv.setImageResource(R.drawable.kids_paint_hands);
            }else if(v.getId() == R.id.image3){

                iv.setBackground(null);

                iv.setVisibility(View.VISIBLE);
                iv.setImageResource(R.drawable.play_logo);
            }else if(v.getId() == R.id.image4){

                iv.setBackground(null);
                iv.setVisibility(View.VISIBLE);
                iv.setBackgroundColor(Color.BLUE);
            }else if(v.getId() == R.id.image5){

                iv.setBackground(null);

                iv.setVisibility(View.VISIBLE);
                iv.setBackgroundColor(Color.CYAN);
            }else if(v.getId() == R.id.image6){

                iv.setBackground(null);

                iv.setVisibility(View.VISIBLE);
                iv.setBackgroundColor(Color.MAGENTA);
            }else if(v.getId() == R.id.image7){

                iv.setBackground(null);

                iv.setVisibility(View.VISIBLE);
                iv.setBackgroundColor(Color.WHITE);
            }else if(v.getId() == R.id.image8){

                iv.setBackground(null);

                iv.setVisibility(View.VISIBLE);
                iv.setBackgroundColor(Color.DKGRAY);
                iv.clearFocus();
            }






        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

有什么帮助或建议吗?

【问题讨论】:

  • 发布您的代码,您可以将图像的可见性设置为消失
  • 如果您在按钮单击中切换全屏,请添加您的代码,以便我们可以看到您的错误,并帮助您。
  • @Pragnani:刚刚用 java 类更新了我的代码

标签: android imageview imagebutton


【解决方案1】:

试试这个

第一

ImageResource is different from the background Resource.

ImageResource is the foreground image, background resource is the background one.

你需要这样做

image.setImageResource(android.R.color.transparent);

显示透明图像而不是原始图像

【讨论】:

    【解决方案2】:

    您的图像显示为背景更改,因为当您将背景颜色更改为粉红色或任何其他纯色时,您没有重置 ImageResource 属性,因此您之前的图像也确实显示为新背景。

    要解决这个问题,您可以将图像显示为背景而不是使用 ImageResource,或者您应该删除之前设置的 imageResource(例如:iv.setImageResource(任何透明框架))。

    【讨论】:

    • 是的,我明白了,但是如何?选项 iv.setImageResource() 仅接受,其余所有不接受 Imageview。
    • Thankyou Rekha,.上述@Pragnani 的方法有效,你的也完全一样......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多