One of the four implementation methods for the Android custom "image + text" control -------- Gallery principle (source code download is provided)

Source: Internet
Author: User

There are four ways to implement the composite controls of image and text. The first method is to use Gallery. Part 1: Create a layout file to add text to images. Name: pic_text.xml, content: [html] <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <ImageView android: id = "@ + id/image" android: layout_gravity = "center_horizontal" android: layout_width = "80dp" android: layout_height = "80dp"/> <TextView android: id = "@ + id/text" android: layout_width = "wrap _ Content "android: layout_height =" wrap_content "android: layout_gravity =" center_horizontal "android: textSize =" 14dp "android: gravity =" center "android: textColor = "# ffffffff"/> </LinearLayout> Part 2: Layout file of the entire program, that is, a gallery: [html] <? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "@ string/hello"/> <Gallery android: id = "@ + id/myGallery" android: layout_width = "fill_parent" android: layout_height = "wrap_content "/> </LinearLayout> Part 3: Main Program: [java] package yan. guoqi. testgallery; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. view; import android. view. viewGroup; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. baseAdapter; import android. widget. gallery; import android. widget. imageView; im Port android. widget. textView; import android. widget. toast; public class TestGalleryActivity extends Activity {/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); Gallery gallery = (Gallery) findViewById (R. id. myGallery); gallery. setAdapter (new galleryAdapter (this); gallery. s EtOnItemClickListener (new OnItemClickListener () {public void onItemClick (AdapterView <?> Parent, View v, int position, long id) {// TODO Auto-generated method stub Toast. makeText (TestGalleryActivity. this, "" + id + "clicked! ", Toast. LENGTH_SHORT ). show () ;}}); gallery. setSelection (1); gallery. setSpacing (20); gallery. setUnselectedAlpha (1500000f);} public class galleryAdapter extends BaseAdapter {private Integer [] img = {R. drawable. identify, R. drawable. recognize, R. drawable. manage}; private String [] str = {"authentication module", "recognition module", "Management Palm Library"}; private Context mContext; public galleryAdapter (Context c) {mContext = c;} public int getCount () {// TODO Auto-generated method stub return img. length;} public Object getItem (int position) {// TODO Auto-generated method stub return position;} public long getItemId (int position) {// TODO Auto-generated method stub return position;} public View getView (int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stub ViewHolder holder; if (convertView = null) {holder = new ViewHolder (); convertView = View. inflate (mContext, R. layout. pic_text, null); holder. pic = (ImageView) convertView. findViewById (R. id. image); holder. text = (TextView) convertView. findViewById (R. id. text); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag ();} holder. pic. setImageResource (img [position]); holder. text. setText (str [position]); return convertView;} www.2cto.com class ViewHolder {private ImageView pic; private TextView text ;}}} (the figure capture is a little small, ⊙ B Khan):

Related Article

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.