Portal☞Wheel Column☞Reprinted please note☞Http://blog.csdn.net/leverage_1229
Today, we will learn how to customize the textview component so that it can display both text and images to achieve the "graphic text" effect. This image and text mashups is often used to present news, articles, MMS and other content. The following is a case of this scenario:
1 case technical points
1.1 create an attrs. xml file to set attributes, types, and styles of custom components.
1.2 use the Android. content. res. typedarray class to load the custom components to the program for calling.
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.customTextView);
1.3 The following settings are required for introducing custom components to the layout file:
Custom component namespace:
xmlns:custom="http://schemas.android.com/apk/res/com.custom.textview"
Custom component labels:
<com.custom.textview.CustomTextView .../>
1.4 construct a hashmap data structure to save the content type and value of the custom component.
Key: Custom component content type (image, text)
Value: Custom component content value (imageurl, charsequence)
1.5 The Android. widget. linearlayout. layoutparams class is used to set the layout parameters of a component. You need to dynamically set the layout parameters of the component based on the type of the displayed content.
2. Case code Display
2.1 project package directory
2.2androidmanifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.custom.textview" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
2.3strings.xml
<Resources> <string name = "app_name"> Custom textview implementation: illustrated </string> </resources>
2.4 property type style file of the custom textview component: attrs. xml
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="customTextView"> <attr name="image_width" format="dimension" /> <attr name="image_height" format="dimension" /> <attr name="text_color" format="color" /> <attr name="text_size" format="dimension" /> </declare-styleable></resources>
2.5main.xml
<?xml version="1.0" encoding="utf-8" ?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res/com.custom.textview" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@android:color/white" > <com.custom.textview.CustomTextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" custom:image_width="200dp" custom:image_height="50dp" /></LinearLayout>
2.6 custom component class: customtextview. Java
Package COM. custom. textview; import java.net. URL; import Java. util. arraylist; import Java. util. hashmap; import android. content. context; import android. content. res. typedarray; import android. graphics. drawable. drawable; import android. OS. handler; import android. OS. message; import android. OS. systemclock; import android. text. HTML; import android. util. attributeset; import android. view. gravity; import android. widget. imageview; import android. widget. linearlayout; import android. widget. textview; public class customtextview extends linearlayout {private context; private typedarray; private layoutparams Params; Public customtextview (context) {super (context);} public customtextview (context, attributeset attrs) {super (context, attrs); this. context = context; this. setorientation (linearlayout. vertical); // from attrs. get the custom attribute typedarray = context in XML. obtainstyledattributes (attrs, R. styleable. customtextview);} public void settext (arraylist
2.7mainactivity.java
Package COM. custom. textview; import Java. util. arraylist; import Java. util. hashmap; import android. app. activity; import android. OS. bundle; public class mainactivity extends activity {private final string text = "<p> this year, Zhejiang Satellite TV made a big move with" good voice of China "+ ", its huge influence has directly affected the strategic deployment of "New Year's Eve" for satellite TV. A few days ago, Hunan Satellite TV, the originator of the concept of "cross-Year gala", took the lead in saying "withdraw from the cross-year war of burning money ". "+" However, according to insiders of Hunan Satellite TV, even so, this year's Hunan New Year's gala will show the concept of "New Year's Eve" + ", "That is, from December 27-12 to 31, for five consecutive days, we will successively use the special programs of "Hundred changes to big coffee show", "Happy base camp" + "," Women like songs ", and" every day "to continuously create this" Season "concept, until the evening of March December 31." </P> "; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // arraylist 3. Case Study