How to use Shape as drawable in imagespan

Asked

Viewed 51 times

2

I have a Pan image, but it only works with images, when I try to use a Hape the location of the image is as if n has no image, but also does not present any error.

       String text = exercicios.get(0).getPergunta();
    Log.i(TAG, exercicios.get(0).getPergunta());
    //String text = "texto # texto";

    //Posição onde colocar a imegem(posição da marca)
    int imagePos = text.indexOf("#");

    //Criar um SpannableString do texto
    SpannableString spannableString = new SpannableString(text);

    //Obter o drawable a inserir
    Drawable drawable = getResources().getDrawable(R.drawable.shapepergunta);
    drawable.setBounds(0, 0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());

    //Criar um ImageSpan do drawable
    ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
    //Inserir a imagem(ImageSpan) no texto(SpannableString)
    spannableString.setSpan(imageSpan,imagePos,imagePos+1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    //Atribuir o texto com a imagem ao TextView
    txvpergunta.setText(spannableString);

Shape:

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="14dp"
/>
<gradient
android:angle="45"
android:centerX="35%"
android:centerColor="#198CFF"
android:startColor="#449DEF"
android:endColor="#2F6699"
android:type="linear"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<stroke
android:width="1dp"
android:color="#878787"
/>
</shape>
  • Put the Shape xml.

  • posted @ramaral

1 answer

1


As the Shape is declared it has no dimensions.

When it’s used as background or foreground other’s view no problem, since it fits the view dimension.

When used as span this does not happen. So you have to define its dimensions.

You can do it in two ways:

  • Including the attribute size in xml:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
        <size
            android:width="32dp"
            android:height="16dp"/>
        <corners
            android:radius="14dp"/>
        <gradient
            android:angle="45"
            android:centerX="35%"
            android:centerColor="#198CFF"
            android:startColor="#449DEF"
            android:endColor="#2F6699"
            android:type="linear"/>
        <padding
            android:left="0dp"
            android:top="0dp"
            android:right="0dp"
            android:bottom="0dp"/>
        <stroke
            android:width="1dp"
            android:color="#878787"/>
    </shape>
    
  • In java through setBounds():

    //Obter o drawable a inserir
    Drawable drawable = getResources().getDrawable(R.drawable.shapepergunta);
    drawable.setBounds(0, 0, 64, 32);
    
  • I managed to do using setbounds, direct in xml, not well aligned vertically, a doubt that I was left is that the drawable some when the bottom value of setbound is less than 3

  • I suppose it’s because I’m too small to be represented.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.