2013-06-25 87 views
1

我正在嘗試將圖像放入TextView。我使用圖像跨度做到了,但是我的問題是我不能在每個圖像中放置onClickListener(在同一個TextView中,在同一個TextView中有多個圖像)。請告訴我該怎麼做。TextView中的圖像可點擊

+1

你有多個圖片在單個文本視圖中,您需要處理所有圖像上的點擊? – blganesh101

+0

你是指可繪製的左邊?發佈代碼! – Sandy09

+1

'\t \t SpannableStringBuilder spanImage = new SpannableStringBuilder(「hello this is befour image」); \t \t spanImage.append(「\ n」); \t \t is = new ImageSpan(this,R.drawable.ic_launcher); spanImage.setSpan(is,header.length()+ 1,header.length()+ 2,Spannable.SPAN_INCLUSIVE_EXCLUSIVE); \t \t spanImage.append(「\ n this after after image」); \t \t tVImage.setText(spanImage,BufferType.SPANNABLE); \t \t' –

回答

0

改爲自定義視圖。這會容易得多。

+0

因爲我是Android新手,這對我來說非常困難。 :( –

1

做出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> 

然後main.xml中,包括本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> 

這是我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 
    } 

    } 
}