简体   繁体   English

Android在TextView上显示图片点击

[英]Android show image on TextView click

This is probably really easy for most of you guys, but since I'm still a beginner I'm having problems figuring it out. 对于大多数人来说,这可能真的很容易,但是由于我仍然是初学者,因此在解决它时遇到了问题。 Anyway, I want a picture to show when a textview is clicked. 无论如何,我希望单击textview时显示图片。 I know how to set up the onClickListener, but dont know how to make the picture show up as the result of the text being clicked. 我知道如何设置onClickListener,但不知道如何使图片显示为单击文本的结果。 What method would I use for it? 我将使用哪种方法? Its a fixed image that I have in my drawable folder. 它是我在可绘制文件夹中的固定图像。 Thanks 谢谢

You can create an ImageView inside your layout where you want to show your image and you can set imageView's visibility attribute to invisible to make it invisible to the screen. 您可以在要显示图像的布局内创建ImageView,还可以将imageView的可见性属性设置为invisible以使其对屏幕不可见。

example: 例:

<ImageView
    android:id="@+id/imageViewCompass"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/mwImage"
    android:visibility="invisible" />

Inside the OnClick of your button you can then set the Imageview visility to visible; 然后,您可以在按钮的OnClick内部将Imageview可见性设置为visible;

yourImageView.setVisibility(View.VISIBLE);

After you inserted your image into drawables, set an ImageView in your .xml as: 将图像插入可绘制对象后,在.xml中将ImageView设置为:

<ImageView
android:layout_width=""
android:layout_height=""
android:id="@+id/image"
android:src="@drawable/imagelocation"
android:visibility="invisible" />

Then in your class Activity, do this: 然后在您的班级活动中,执行以下操作:

public class Templates extends Activity {

Button btnimage;
ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

image = (ImageView) findViewById(R.id.image);
btnimage = (Button) findViewById(R.id.btnimage);

btnimage.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

image.setVisibility(View.VISIBLE);

        }
    });
}

UPDATE: 更新:

Yes, it is possible to show up ImageView on dialog. 是的,可以在对话框上显示ImageView。 Follow this: 请遵循以下步骤:

Dialog settingsDialog = new Dialog(this);
settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.image_layout, null));
settingsDialog.show();

Then your .xml should look something like: 然后,您的.xml应该如下所示:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="YOUR IMAGE"/>
<Button 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:text="Show Image" android:onClick="dismissListener"/>
</LinearLayout>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM