My Android advanced tour ------> Android's preliminary understanding of ImageSpan and SpannableString, androidimagespan
Recently, we want to implement a QQ chat input box that allows you to simultaneously input text and emoticon images. As shown in:
To achieve this effect, I first learned how to use ImageSpan and SpannableString. Let's take a look at the specific usage using a small instance. The effect is as follows:
The Code is as follows:
@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); TextView mDetailView = (TextView) findViewById (R. id. detail); String text = "Hello everyone, smile a little, like this [smile]"; ImageSpan imageSpan = new ImageSpan (this, R. drawable. smile); SpannableString spannableString = new SpannableString (text); spannableString. setSpan (imageSpan, text. indexOf ('['), text. indexOf (']') + 1, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE); mDetailView. setText (spannableString );}
In the code, replace the [smile] text with the R. id. smile image.
Layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/detail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /></RelativeLayout>
========================================================== ========================================================== ============================
To achieve the effect of the QQ chat input box shown in the first figure, you need to find multiple QQ emoticons and store them with a Map <String key, Integer value> corresponding text. Then, you can use the grid layout to scale all the expressions and then display them. You also need to use the paging technology and add special effects on the three-dimensional flip effect. No code will be posted here.
Refer to related blogs on the Internet:
Android UI [android imitation, QQ chat, with emoticons, flip pages, drag cache with flip pages]
Address: http://blog.csdn.net/lnb333666/article/details/8546497
========================================================== ========================================================== ============================
Author: Ouyang Peng: Welcome to repost. sharing with others is the source of progress!
Reprinted Please retain the original address: http://blog.csdn.net/ouyang_peng
========================================================== ========================================================== ============================
Copyright statement: This article is the original author article, welcome to reprint, reprint please indicate the source of http://blog.csdn.net/ouyang_peng