简体   繁体   中英

Edit text view with image and text wrapped around image

I am building a rich text editor. I have implemented text formatting like bold italic etc and also paragraph formatting like blockQuote. Now I would like to add images in editor and text should wrap around it. I have implemented all these using SpannableString() and Spanned() and StyleSpan() .

I can add image to a line using ImageSpan() , but that add it inline and its just there in place of a character , what I want is to insert it in paragraph and rest of text should wrap around it. I am able to add image at the beginning of text by following code.. but I cannot align it center and right.

 SpannableString string = new SpannableString("Text with icon and padding");
 string.setSpan(new IconMarginSpan(bitmap, 30), 0, string.length(),
 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

How to do it ? any example? or what procedure to follow?

you can fulfill your requirement like below:

you can set your image in imageview and your text in Html.fromHtml("here put your text")

gradle file:

compile 'com.github.deano2390:FlowTextView:2.0.5' 

your XML layout:

<uk.co.deanwild.flowtextview.FlowTextView
    android:id="@+id/ftv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginTop="400dip"
            android:padding="10dip"
            android:src="@drawable/android2"/>

</uk.co.deanwild.flowtextview.FlowTextView>

your java file:

FlowTextView flowTextView = (FlowTextView) findViewById(R.id.ftv);
             Spanned html = Html.fromHtml("here put your text");
             flowTextView.setText(html);

you may get help by this link https://github.com/deano2390/FlowTextView also

I hope it help you..!

As per details given by you, It seems you might be using native EditText or you are trying with your custom EditText. Please provide this details to get exact solution of your query.

Apart from that, To build Rich Text Editor with the features mentioned by you (like Bold, Italic, Quote and Images etc), you can use this library RichEditor for Android: https://github.com/wasabeef/richeditor-android . It provides many features which may interest you.

Try with:

EditText text = (EditText)findViewById(R.id.text);
text.setCompoundDrawables(null, null, getResources().getDrawable(R.drawable.check_box), null);

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