Android Custom "picture + text" Control of the four methods of implementation of the two--------personal most recommended one

Source: Internet
Author: User

http://blog.csdn.net/yanzi1225627/article/details/8633872

The second method is to create a new image + text XML layout file, and then write a class that inherits from LinearLayout. Instantiate and set the corresponding parameters in the main program. This way is also my most recommended one.

The first part: Myimgbtn_layout.xml

[HTML]View Plaincopyprint?
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:alpha="
  6. android:background="#87CE"
  7. android:orientation="vertical"
  8. >
  9. <ImageView
  10. android:id="@+id/img"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_gravity="Center_horizontal"
  14. android:paddingbottom="5dip"
  15. android:paddingtop="5dip" />
  16. <TextView
  17. android:id="@+id/text"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:textcolor="#FF6100"
  21. android:textsize="30dip"
  22. android:layout_gravity="center_vertical"/>
  23. </linearlayout>


The second part, corresponding to the layout of the Myimgbtn.java file:

[Java]View Plaincopyprint?
  1. Package yan.guoqi.testimgbtn;
  2. Import Android.content.Context;
  3. Import Android.graphics.Color;
  4. Import Android.util.AttributeSet;
  5. Import Android.view.LayoutInflater;
  6. Import android.view.MotionEvent;
  7. Import Android.view.View;
  8. Import Android.view.View.OnTouchListener;
  9. Import Android.widget.ImageView;
  10. Import Android.widget.LinearLayout;
  11. Import Android.widget.TextView;
  12. Public class Myimgbtn extends LinearLayout {
  13. private ImageView Mimgview = null;
  14. private TextView Mtextview = null;
  15. private Context Mcontext;
  16. Public myimgbtn (context context, AttributeSet attrs) {
  17. Super (context, attrs);
  18. //TODO auto-generated constructor stub
  19. Layoutinflater.from (context). Inflate (R.layout.myimgbtn_layout, this , true);
  20. Mcontext = context;
  21. Mimgview = (ImageView) Findviewbyid (r.id.img);
  22. Mtextview = (TextView) Findviewbyid (R.id.text);
  23. }
  24. / * Set Picture interface * /
  25. public void Setimageresource (int resId) {
  26. Mimgview.setimageresource (RESID);
  27. }
  28. / * Set the Text interface * /
  29. public void SetText (String str) {
  30. Mtextview.settext (str);
  31. }
  32. / * Set the text size * /
  33. public void settextsize (float size) {
  34. Mtextview.settextsize (size);
  35. }
  36. /* Set Touch interface */
  37. public void Setontouch (Ontouchlistener listen) {
  38. Mimgview.setontouchlistener (listen);
  39. Mtextview.setontouchlistener (listen);
  40. //    }
  41. }


The third part, the main layout main.xml:

[HTML]View Plaincopyprint?
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. android:background="@drawable/main_background2">
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello" />
  11. <yan.guoqi.testimgbtn.MyImgBtn
  12. android:id="@+id/myibtn_1"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_gravity="Center_horizontal"
  16. android:clickable="true"
  17. android:focusable="true"
  18. />
  19. </linearlayout>

Part IV, main program:

[Java]View Plaincopyprint?
  1. Package yan.guoqi.testimgbtn;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import Android.view.View;
  5. Import Android.view.View.OnClickListener;
  6. Import Android.widget.Toast;
  7. Public class Testimgbtnactivity extends Activity {
  8. private MYIMGBTN MyIBtn1 = null;
  9. /** Called when the activity is first created. * /
  10. @Override
  11. public void OnCreate (Bundle savedinstancestate) {
  12. super.oncreate (savedinstancestate);
  13. Setcontentview (R.layout.main);
  14. MYIBTN1 = (myimgbtn) Findviewbyid (r.id.myibtn_1);
  15. Myibtn1.setimageresource (R.drawable.ic_launcher);
  16. Myibtn1.settext ("Welcome");
  17. Myibtn1.settextsize (24.0f);
  18. //myibtn1.setontouch (New Myontouchlistener ());
  19. Myibtn1.setonclicklistener (new Onclicklistener () {
  20. public void OnClick (View arg0) {
  21. //TODO auto-generated method stub
  22. Toast.maketext (testimgbtnactivity. This,
  23. "Hello",
  24. Toast.length_short)
  25. . Show ();
  26. }
  27. });
  28. }
  29. }


This method is straightforward and easy to understand with the first method of gallery. is to customize a class, although the first method does not use a custom class, but the gallery related adapter configuration and that view related if the first time will not be very accustomed. This effect is also good, the picture is not affixed. Especially suitable for doing that background is solid color, inside nested picture + text. IS 360 mobile phone security defender's main window, you can look down. It should be done in this way. In the ointment is, 360 mobile phone Security defender's main window, you click, the background will change. That means there's still a ontouchlistener missing, and I'll make it up later.

Android Custom "picture + text" Control of the four methods of implementation of the two--------personal most recommended one

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.