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. The name is pic_text.xml and the content is:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><ImageViewandroid:id="@+id/image"android:layout_gravity="center_horizontal"android:layout_width="80dp"android:layout_height="80dp"/><TextViewandroid: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:

<?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:

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; import android. widget. textview; import android. wi DGET. 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) findviewbyid (R. id. mygallery); Gallery. setadapter (New galleryadapter (this); Gallery. setonitemclicklistener (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;} class viewholder {private imageview PIC; private textview text ;}}}

(The figure is a little small, and the figure is very sweaty ):

Analysis: This method uses gallery to customize a layout. Writing an adapter on your own is not bad for learning Gallery. The effect can be slide. But it is not recommended to write it like this. It seems that gallery is used in upper case. I strongly recommend writing in the next blog.

Source -- http://download.csdn.net/detail/yanzi1225627/5108013

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.