[英]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: 这是我要做的:
Make your layout a RelativeLayout
instead of a TableLayout
(this will make things easier for showing the image in the center) 将您的布局设为
RelativeLayout
而不是TableLayout
(这将使在中心显示图像更加容易)
Place your TableLayout
within the wrapping RelativeLayout
将您的
TableLayout
放在包装的RelativeLayout
Define an ImageView
as the last child within the wrapping RelativeLayout
, set centerInParent="true"
, visibilty="gone"
将
ImageView
定义为包装的RelativeLayout
的最后一个子级,设置centerInParent="true"
, visibilty="gone"
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
,则可以通过编程方式创建它:
Follow the same steps 1-2 as before 遵循与之前相同的步骤1-2
Capture the reference to the wrapping RelativeLayout
in the code 在代码中捕获对包装的
RelativeLayout
的引用
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"
(如果您想要有关如何执行此操作的示例,请告诉我,我将用代码示例编辑答案)。
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.