Android custom "image + text" controls: two of the four implementation methods-one of my most recommended

Source: Internet
Author: User

The second method also creates an XML layout file for images and text, and then writes a class inherited from linearlayout. Instantiate and set relevant parameters in the main program. This method is also the most recommended one.

Part 1: myimgbtn_layout.xml

<?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:alpha="20"    android:background="#87CE"    android:orientation="vertical"     >    <ImageView        android:id="@+id/img"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:paddingBottom="5dip"        android:paddingTop="5dip" />    <TextView         android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"           android:textColor="#FF6100"           android:textSize="30dip"          android:layout_gravity="center_vertical"/></LinearLayout>

The second part is the myimgbtn. Java file corresponding to the layout:

Package Yan. guoqi. testimgbtn; import android. content. context; import android. graphics. color; import android. util. attributeset; import android. view. layoutinflater; import android. view. motionevent; import android. view. view; import android. view. view. ontouchlistener; import android. widget. imageview; import android. widget. linearlayout; import android. widget. textview; public class myimgbtn extends linearlayout {private imageview mimgview = NULL; private textview mtextview = NULL; private context mcontext; Public myimgbtn (context, attributeset attrs) {super (context, attrs); // todo auto-generated constructor stub layoutinflater. from (context ). inflate (R. layout. myimgbtn_layout, this, true); mcontext = context; mimgview = (imageview) findviewbyid (R. id. IMG); mtextview = (textview) findviewbyid (R. id. text);}/* set image interface */Public void setimageresource (INT resid) {mimgview. setimageresource (resid);}/* set the text interface */Public void settext (string Str) {mtextview. settext (STR);}/* set the text size */Public void settextsize (float size) {mtextview. settextsize (size);} // * set the touch interface * // public void setontouch (ontouchlistener listen) {// mimgview. setontouchlistener (Listen); // mtextview. setontouchlistener (Listen );//}}

Part 3: Main. xml:

<?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"     android:background="@drawable/main_background2">    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" />    <yan.guoqi.testimgbtn.MyImgBtn        android:id="@+id/MyIBtn_1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"                android:clickable="true"        android:focusable="true"              /></LinearLayout>

Part 4: Main Program:

Package Yan. guoqi. testimgbtn; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. toast; public class testimgbtnactivity extends activity {private myimgbtn myibtn1 = NULL;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); myibtn1 = (myimgbtn) findviewbyid (R. id. myibtn_1); myibtn1.setimageresource (R. drawable. ic_launcher); myibtn1.settext ("welcome"); myibtn1.settextsize (24366f); // listener (New myontouchlistener (); listener (New onclicklistener () {public void onclick (view arg0) {// todo auto-generated method stub toast. maketext (testimgbtnactivity. this, "hello", toast. length_short ). show ();}});}}

This method is intuitive and simple, which is easier to understand than the first method using Gallery. It is to customize a class. Although the first method does not need to define a class, the gallery-related adapter configuration is related to that view. If it is not used for the first time. This effect is also good, and the figure will not be pasted. It is especially suitable for solid color backgrounds with nested images and text. It is the main window of 360 mobile security guard. You can see it. This method should be used. In the US, if you click in the main window of 360 mobile security guard, the background will change. In other words, this still lacks an ontouchlistener. I will add it later.

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.