My Android advanced tour ------ & gt; Android's preliminary understanding of ImageSpan and SpannableString, androidimagespan

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.