12
votes

Comment insérer ImageView à la fin de Multiline TextView?

Peut-être que c'est une question stupide, mais je ne peux pas trouver un moyen d'insérer une petite image à la fin de la deuxième ligne de TextView. L'image est toujours apparaît à droite de TextView, pas la ligne de ligne.

Je veux insérer une image comme ceci: xxx

Ce que je reçois est: xxx

J'espère qu'il y a un moyen de le faire.

src: xxx


1 commentaires

Donnez votre code XML, plus facile de voir le problème.


9 Réponses :


0
votes

Ce n'est peut-être pas la meilleure solution, mais vous pouvez essayer d'utiliser html.fromhtml pour insérer une image dans votre ligne.

ImageGetter getter = new ImageGetter(){                 
    public Drawable getDrawable(String source){
        Drawable d = null;

        context.getResources().getDrawable(Integer.parseInt(source));   

        if (d != null)
            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());

        return d;
    }
};
String imgString = <source string> + " <img src=\"R.drawable.icon\"/>";
((TextView)findViewById(R.id.tv)).setText(
    Html.fromHtml(imgString, getter, null));


0 commentaires

15
votes

Créez une imagePan et ajoutez-le à votre TextView. De cette façon, vous pouvez mettre votre image où vous voulez dans le texte

exemple - xxx


7 commentaires

Imagespan imagespan = Nouvelle imagespan (AppContext, Ressource); text.setspan (imagespan, i, i + strlength, 0);


Essayez de créer de nombreuses étendues et de les concaténer à l'aide de textutils.concat ()


Il n'y a pas de méthode TextView.setspan.


@Discdev parce que vous essayez d'appliquer SpotSpan vers un TextView au lieu d'un chiffon


@erdomester yup. Le commentaire dans la Post de l'OP indique que «Texte est un objet de TextView» ... Évidemment, ce n'est pas parce que cela ne compilerait pas.


Spannablestring SS = Nouveau SpannaNestring (titre); DITABLE D = GETRESOURCES (). GETDRAWABLE (R.DRAWABLE.GALLERY_LIST); d.setbounds (0, 0, D.GetIntrinsicWidth (), D.GetIntrinsicheightight ()); Imagespan span = nouvelle imagespan (D, imagespan.align_baseline); Ss.setspan (Span, 0, 3, Spannable.Span_Exclusive_Inclusive); NewstextView.SetText (SS);


@Budagavril Après avoir utilisé cette solution Setontouchlistener pour TextView Drakable ne fonctionnera pas!



5
votes

Un meilleur moyen de terminer est comme suit ...

ImageGetter getter = new ImageGetter(){                 
    public Drawable getDrawable(String source){   // source is the resource name
        Drawable d = null;
        Integer id =  new Integer(0);
        id = getApplicationContext().getResources().getIdentifier(source, "drawable", "com.sampleproject");

        d = getApplicationContext().getResources().getDrawable(id);   

        if (d != null)
            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());

        return d;
    }
};

String imgString = this.getResources().getString(R.string.text_string) + " <img src=\"img_drawable_image\"/>";
((TextView)findViewById(R.id.textview_id)).setText(
Html.fromHtml(imgString, getter, null));


0 commentaires

1
votes
SpannableString ss = new SpannableString(title);
Drawable d = getResources().getDrawable(R.drawable.gallery_list);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
ss.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
newsTextView.setText(ss);

0 commentaires

1
votes
TextView textView =new TextView(this);
SpannableStringBuilder ssb = new SpannableStringBuilder( "Here's a smiley how are you " );
Bitmap smiley = BitmapFactory.decodeResource( getResources(), R.drawable.movie_add );
ssb.setSpan( new ImageSpan( smiley ), ssb.length()-1,  ssb.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE );  
textView.setText( ssb, BufferType.SPANNABLE );

0 commentaires

0
votes

Merci à libin Thomas J'ai créé une solution pour Barre de notation . J'ai utilisé SVGratingBar avec SVG Stars.

imagespan étend dynamicDrawableespan et dynamicDrawableespan n'a qu'un seul enfant - imagespan . Donc, pour attacher une autre page à la fin du textview Nous devrions obtenir un diguable fabriqué à partir de la vue . J'ai ajouté une méthode getdrawable () à svgradingbar : xxx

créer une mise en page pour la note (layout_rating_bar.xml) : xxx

puis écrivez ce code: xxx

 Entrez la description de l'image ici


0 commentaires

1
votes

Si vous souhaitez insérer diguable à la fin du texte, vous cachez le dernier caractère du texte pour éviter d'ajouter un autre caractère à la fin du texte et de démarrer le dessinable à ce caractère.

val myText = "Your text"    

val span: Spannable = SpannableString(myText+"-")
val android: Drawable = ContextCompat.getDrawable(this, R.drawable.yourDrawable)!!
android.setBounds(0, 0, 30, 30)
val image = ImageSpan(android, ImageSpan.ALIGN_BOTTOM)
span.setSpan(image, span.indexOf("-"), span.indexOf("-")+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)


0 commentaires

0
votes

rallongement kotlin sur le textview classe xxx

appelez-le après vous définissez le texte sur le textview et c'est tout.


0 commentaires

1
votes

Vous pouvez ajouter l'image à la fin de la chaîne sans utiliser imageview comme ci-dessous; xxx pré>

et appelez-le comme; p>

textView.text = addImageToEndOfTheString("Your text is here", R.drawable.ic_your_image, context!!)


0 commentaires