• 自定义带图片和文字的ImageTextButton


    今天我们来讲一下有关自定义控件的问题,今天讲的这篇是从布局自定义开始的,难度不大,一看就明白,估计有的同学或者开发者看了说,这种方式多此一举,但是小编我不这么认为,多一种解决方式,就多一种举一反三的学习。下一次或者过几天我会从自定义属性,在布局文件中使用属性的方式再讲一篇关于自定义控件的文章,希望对大家能够有所帮助。


    现在开始讲自定义带图片和文字的ImageTextButton的实现方法。

    效果图如下:


    第一步:新建一个image_text_buttton.xml的布局文件,供自定义的控件使用

    [html] view plain copy
    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.   
    7.     <ImageView  
    8.         android:id="@+id/iv"  
    9.         android:layout_width="wrap_content"  
    10.         android:layout_height="wrap_content"  
    11.         android:layout_gravity="center_horizontal"  
    12.         android:layout_marginTop="12dp" />  
    13.   
    14.     <TextView  
    15.         android:id="@+id/tv"  
    16.         android:layout_width="wrap_content"  
    17.         android:layout_height="wrap_content"  
    18.         android:layout_gravity="center_horizontal"  
    19.         android:layout_marginTop="8dp"  
    20.         android:textColor="#000000" />  
    21.   
    22. </LinearLayout>  

    第二步:自定义一个类ImageTextButton继承LinearLayout

    [java] view plain copy
    1. package net.loonggg.itbd.view;  
    2.   
    3. import net.loonggg.itbd.R;  
    4. import android.content.Context;  
    5. import android.util.AttributeSet;  
    6. import android.view.LayoutInflater;  
    7. import android.widget.ImageView;  
    8. import android.widget.LinearLayout;  
    9. import android.widget.TextView;  
    10.   
    11. public class ImageTextButton extends LinearLayout {  
    12.     private ImageView iv;  
    13.     private TextView tv;  
    14.   
    15.     public ImageTextButton(Context context) {  
    16.         super(context);  
    17.     }  
    18.   
    19.     public ImageTextButton(Context context, AttributeSet attrs) {  
    20.         super(context, attrs);  
    21.         LayoutInflater.from(context).inflate(R.layout.image_text_buttton, this,  
    22.                 true);  
    23.         iv = (ImageView) findViewById(R.id.iv);  
    24.         tv = (TextView) findViewById(R.id.tv);  
    25.     }  
    26.   
    27.     public void setDefaultImageResource(int resId) {  
    28.         iv.setImageResource(resId);  
    29.     }  
    30.   
    31.     public void setDefaultTextViewText(String text) {  
    32.         tv.setText(text);  
    33.     }  
    34.   
    35.     /** 
    36.      * @param resId 
    37.      */  
    38.     public void setImageResource(int resId) {  
    39.         iv.setImageResource(resId);  
    40.     }  
    41.   
    42.     /** 
    43.      * @param text 
    44.      */  
    45.     public void setTextViewText(String text) {  
    46.         tv.setText(text);  
    47.     }  
    48.   
    49.     /** 
    50.      * @param color 
    51.      */  
    52.     public void setTextColor(int color) {  
    53.         tv.setTextColor(color);  
    54.     }  
    55.   
    56. }  

    第三步:自定义控件的使用,在布局文件activity_main.xml中的使用

    [html] view plain copy
    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.   
    11.     <net.loonggg.itbd.view.ImageTextButton  
    12.         android:id="@+id/itb"  
    13.         android:layout_width="wrap_content"  
    14.         android:layout_height="wrap_content" />  
    15.   
    16. </RelativeLayout>  

    第四步:在Activiy中的使用

    [java] view plain copy
    1. public class MainActivity extends Activity {  
    2.     private ImageTextButton itb;  
    3.   
    4.     @Override  
    5.     protected void onCreate(Bundle savedInstanceState) {  
    6.         super.onCreate(savedInstanceState);  
    7.         setContentView(R.layout.activity_main);  
    8.   
    9.         itb = (ImageTextButton) findViewById(R.id.itb);  
    10.         itb.setImageResource(R.drawable.sure);  
    11.         itb.setTextViewText("确定");  
    12.     }  
    13.   
    14. }  
  • 相关阅读:
    tcl基本语法
    linux hostid与lmhostid
    linux下uptime命令
    介绍一下 WebApplicationContext?
    Spring 由哪些模块组成?
    怎样开启注解装配?
    解释不同方式的自动装配 ?
    @Qualifier 注解有什么用?
    解释 Spring 框架中 bean 的生命周期?
    如何在 spring 中启动注解装配?
  • 原文地址:https://www.cnblogs.com/wxmdevelop/p/5286631.html
Copyright © 2020-2023  润新知