Customize Imagetextbutton with pictures and text

Source: Internet
Author: User

Today we talk about the problem of custom control, today, this is from the layout of the custom start, the difficulty is not very difficult, a look at it, estimated that some students or developers to see that this way superfluous, but small series I do not think so, more than a solution, more than a extrapolate of learning. Next time, or in a few days, I'm going to start with a custom property, using attributes in a layout file, and I want to be able to help you with an article about custom controls.


now it's time to start customizing the implementation of Imagetextbutton with pictures and text.

As follows:


First step: Create a new Image_text_buttton.xml layout file for your custom control to use

[HTML]View PlainCopy
  1. <? XML version="1.0" encoding="Utf-8"?>
  2. < LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android"
  3. android:layout_width="Wrap_content"
  4. android:layout_height="Wrap_content"
  5. android:orientation="vertical" >
  6. < ImageView
  7. android:id="@+id/iv"
  8. android:layout_width="Wrap_content"
  9. android:layout_height="Wrap_content"
  10. android:layout_gravity="Center_horizontal"
  11. android:layout_margintop="12DP" />
  12. < TextView
  13. android:id="@+id/tv"
  14. android:layout_width="Wrap_content"
  15. android:layout_height="Wrap_content"
  16. android:layout_gravity="Center_horizontal"
  17. android:layout_margintop="8DP"
  18. Android:textcolor="#000000" />
  19. </ LinearLayout >

Step Two: Customize a class Imagetextbutton inherit LinearLayout

[Java]View PlainCopy
  1. Package Net.loonggg.itbd.view;
  2. Import NET.LOONGGG.ITBD.R;
  3. Import Android.content.Context;
  4. Import Android.util.AttributeSet;
  5. Import Android.view.LayoutInflater;
  6. Import Android.widget.ImageView;
  7. Import Android.widget.LinearLayout;
  8. Import Android.widget.TextView;
  9. public class Imagetextbutton extends LinearLayout {
  10. Private ImageView IV;
  11. Private TextView TV;
  12. Public Imagetextbutton (Context context) {
  13. Super (context);
  14. }
  15. Public Imagetextbutton (context context, AttributeSet Attrs) {
  16. Super (context, attrs);
  17. Layoutinflater.from (context). Inflate (R.layout.image_text_buttton, this,
  18. true);
  19. IV = (ImageView) Findviewbyid (R.ID.IV);
  20. TV = (TextView) Findviewbyid (r.id.tv);
  21. }
  22. public void Setdefaultimageresource (int resId) {
  23. Iv.setimageresource (RESID);
  24. }
  25. public void Setdefaulttextviewtext (String text) {
  26. Tv.settext (text);
  27. }
  28. /**
  29. * @param resId
  30. */
  31. public void Setimageresource (int resId) {
  32. Iv.setimageresource (RESID);
  33. }
  34. /**
  35. * @param text
  36. */
  37. public void Settextviewtext (String text) {
  38. Tv.settext (text);
  39. }
  40. /**
  41. * @param color
  42. */
  43. public void SetTextColor (int color) {
  44. Tv.settextcolor (color);
  45. }
  46. }

Step Three: Use of custom controls in layout file Activity_main.xml

[HTML]View PlainCopy
  1. < Relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"
  2. xmlns:tools="Http://schemas.android.com/tools"
  3. android:layout_width="Match_parent"
  4. android:layout_height="Match_parent"
  5. Android:paddingbottom="@dimen/activity_vertical_margin"
  6. android:paddingleft="@dimen/activity_horizontal_margin"
  7. android:paddingright="@dimen/activity_horizontal_margin"
  8. android:paddingtop="@dimen/activity_vertical_margin"
  9. Tools:context=". Mainactivity " >
  10. < Net.loonggg.itbd.view.ImageTextButton
  11. android:id="@+id/itb"
  12. android:layout_width="Wrap_content"
  13. android:layout_height="wrap_content" />
  14. </ Relativelayout >

Fourth step: use in Activiy

[Java]View PlainCopy
  1. public class Mainactivity extends Activity {
  2. Private Imagetextbutton ITB;
  3. @Override
  4. protected void OnCreate (Bundle savedinstancestate) {
  5. Super.oncreate (savedinstancestate);
  6. Setcontentview (R.layout.activity_main);
  7. ITB = (Imagetextbutton) Findviewbyid (R.ID.ITB);
  8. Itb.setimageresource (r.drawable.sure);
  9. Itb.settextviewtext ("OK");
  10. }
  11. }

Customize Imagetextbutton with pictures and text

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.