简体   繁体   English

Android-从res / drawable onClick显示图像

[英]Android - Show image from res/drawable onClick

First of all, I wanna say I've been seeking for an answer on the Forum and I found didn't match for what I wanted. 首先,我想说我一直在论坛上寻求答案,但发现与我想要的不匹配。 Basically, what I want is: when the user clicks on one of the images previously "specified" on the .xml file, a new image is displayed on the center of the screen that is not "specified" on the .xml file. 基本上,我想要的是:当用户单击.xml文件中先前“指定”的图像之一时,新的图像显示在屏幕的中心,而不是.xml文件中的“指定”。 I wrote "specified" cause idk if it's the correct way to refer to this. 我写了“指定的”原因idk,如果它是引用此问题的正确方法。

EDIT: there was no need to not specify the image previously, all I needed was to set "gone" for visibiity. 编辑:没有必要事先不指定图像,我所需要的只是为可见性设置“消失”。 This code is working exactly how I wanted (ty guys): 这段代码正好按照我想要的方式工作(男):

Main.java Main.java

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

public class Principal extends Activity {
    ImageView cuia1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);
        cuia1 = (ImageView) findViewById(R.id.cuia1);
        cuia1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                ImageView cuia1grande = (ImageView) findViewById(R.id.cuia1grande);
                cuia1grande.setVisibility(1);
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.principal, menu);
        return true;
    }
}

activity.xml activity.xml

    <?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"  
android:id="@+id/relativeLayout1"  
android:layout_width="match_parent"  
android:layout_height="match_parent">  

<TableLayout
    android:id="@+id/tableLayout1"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:shrinkColumns="*"  
    android:stretchColumns="*">   

    <TableRow   
        android:id="@+id/tabelaCuias"  
        android:layout_height="wrap_content"  
        android:layout_width="match_parent"  
        android:gravity="center">    

        <TextView  
            android:id="@+id/selecionaCuia"  
            android:text="Selecione a cuia"  
            android:textStyle="bold">            
        </TextView>  

        <ImageView  
            android:id="@+id/cuia1"  
            android:src="@drawable/cuia1">            
        </ImageView>  

        <ImageView  
            android:id="@+id/cuia2"  
            android:src="@drawable/cuia2">            
        </ImageView>  

        <ImageView  
            android:id="@+id/cuia3"  
            android:src="@drawable/cuia3">            
        </ImageView>  

        <ImageView  
            android:id="@+id/cuia4"  
            android:src="@drawable/cuia4">            
        </ImageView>                         
    </TableRow>  
</TableLayout>

<ImageView  
    android:layout_height="wrap_content"  
    android:layout_width="match_parent"
    android:layout_centerInParent="true"
    android:visibility="gone"
    android:id="@+id/cuia1grande"  
    android:src="@drawable/cuia1grande">            
</ImageView>

Is there any reason you don't want to "specify" the image in your layout file? 您有什么理由不想在布局文件中“指定”图像? You could place it there and not display it ( visibilty="gone" ), and then show/hide it when you deem fit. 您可以将其放置在此处而不显示( visibilty="gone" ),然后在您认为合适时显示/隐藏它。

Here's what I'd do: 这是我要做的:

  1. Make your layout a RelativeLayout instead of a TableLayout (this will make things easier for showing the image in the center) 将您的布​​局设为RelativeLayout而不是TableLayout (这将使在中心显示图像更加容易)

  2. Place your TableLayout within the wrapping RelativeLayout 将您的TableLayout放在包装的RelativeLayout

  3. Define an ImageView as the last child within the wrapping RelativeLayout , set centerInParent="true" , visibilty="gone" ImageView定义为包装的RelativeLayout的最后一个子级,设置centerInParent="true"visibilty="gone"

  4. In your onClick method, simply set its visibility as visible . 在您的onClick方法中,只需将其visibility设置为visible

If you really don't want to define the ImageView in the layout, then you can create it programmatically: 如果您确实不想在布局中定义ImageView ,则可以通过编程方式创建它:

  1. Follow the same steps 1-2 as before 遵循与之前相同的步骤1-2

  2. Capture the reference to the wrapping RelativeLayout in the code 在代码中捕获对包装的RelativeLayout的引用

  3. In the onClick method, create the ImageView programatically, specifying the centerInParent="true" via the code (let me know if you want an example on how to do this & I'll edit the answer with a code sample). onClick方法中,以编程方式创建ImageView ,并通过代码指定centerInParent="true" (如果您想要有关如何执行此操作的示例,请告诉我,我将用代码示例编辑答案)。

  4. Add the new view to the RelativeLayout via myRelativeLayout.addView(myImageView); 通过myRelativeLayout.addView(myImageView);将新视图添加到RelativeLayout myRelativeLayout.addView(myImageView);

Hope this helps :) 希望这可以帮助 :)

public class Principal extends Activity {
    ImageView cuia1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);
        cuia1 = (ImageView) findViewById(R.id.cuia1);
        //set invisible
        cuia1 .setVisibility(View.INVISIBLE);
        cuia1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //show image on the center of screen  
                //set image
                cuia1.setImageResource(R.drawable.cuia1);
                // set visible
                cuia1 .setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {     
        getMenuInflater().inflate(R.menu.principal, menu);
        return true;
    }
}

import import android.view.View; import import android.view.View;

Cheerz! 加油!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将图像文件从res / drawable保存到android中的Gallery App - Saving an image file from res/drawable to Gallery App in android 将图像从SD卡复制到Android应用程序的res / drawable文件夹中 - To copy an image from an sdcard into res/drawable folder of the Android app android 中的 Webview 不会从 res/drawable 加载图像 - Webview in android does not load image from res/drawable Android:下载图片并将其放在res / drawable文件夹中 - Android: download image and put it in the res/drawable folder Android:无法将图片拖到可绘制的资源中 - Android: Can't drag image into res drawable Android-显示资源可绘制图像 - Android - Show image from resource drawable Android:如何从res / drawable检索图像并将其放入WebView.loadUrl() - Android: how to retrieve an image from res/drawable and put it into WebView.loadUrl() 使用buildin Android Image Viewer打开存储在res / drawable中的图片 - Open Picture stored in res/drawable with buildin Android Image Viewer res / drawable中有许多图像文件,如何在Android中访问? - Many image files in res/drawable, How to access in Android? 如何正确地将图像检索到Android项目的res / drawable /文件夹中? - How correctly retrieve an image into the res/drawable/ folder of an Android project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM