简体   繁体   中英

Image in TextView With clickable

I am trying to put image in TextView . I had done it using image span but my problem is i cannot put onClickListener in every image (in same TextView, there are multipal image in same TextView). Please suggest me how can i do that.

make a custom.xml

    <RelativeLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
      android:layout_height="wrap_content">
      <ImageView 
           android:id="@+id/thumbnail_view"
           android:src="@drawable/ic_launcher"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />

<TextView android:id="@+id/message_view"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_toRightOf="@id/thumbnail_view"
           android:textSize="18sp"
           android:text="MyText" />
  </RelativeLayout>

then in main.xml , include this custom.xml

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" 
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"  
      android:gravity="center_horizontal">

     <include 
       android:id="@+id/customView"
       layout="@layout/custom"/>

        </LinearLayout>

This is my mainActivity.class

    package com.example.test;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ImageView;
    import android.widget.TextView;


    public class MainActivity extends Activity implements OnClickListener {

       private String TAG = MainActivity.class.getSimpleName();
       ImageView img;
       ImageView img1;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

      TextView txt = (TextView)findViewById(R.id.message_view);
      img = (ImageView) findViewById(R.id.thumbnail_view);
      img1 = (ImageView) findViewById(R.id.thumbnail_view1);

      img.setOnClickListener(this);
       img1.setOnClickListener(this);

     }
   @Override
   public void onClick(View v) {
      if(v== img){
         // do something for img
     }
       else if (v== img1){
          //do something for img1
     }

  }
 }

make a custom view instead. That would be much easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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