zoukankan      html  css  js  c++  java
  • Android自定义“图片+文字”控件四种实现方法之 二--------个人最推荐的一种

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

    第二种方法也要新建一个图片+文字的xml布局文件,然后写一个类继承自LinearLayout。在主程序里实例化并设置相应参数。这种方式也是我最推荐的一种。

    第一部分:myimgbtn_layout.xml

    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="20"  
    6.     android:background="#87CE"  
    7.     android:orientation="vertical"   
    8.     >  
    9.   
    10.     <ImageView  
    11.         android:id="@+id/img"  
    12.         android:layout_width="wrap_content"  
    13.         android:layout_height="wrap_content"  
    14.         android:layout_gravity="center_horizontal"  
    15.         android:paddingBottom="5dip"  
    16.         android:paddingTop="5dip" />  
    17.     <TextView   
    18.         android:id="@+id/text"  
    19.         android:layout_width="wrap_content"  
    20.         android:layout_height="wrap_content"     
    21.         android:textColor="#FF6100"     
    22.         android:textSize="30dip"    
    23.         android:layout_gravity="center_vertical"/>  
    24.   
    25.   
    26.   
    27. </LinearLayout>  


    第二部分,与之布局相对应的MyImgBtn.java 文件:

    1. package yan.guoqi.testimgbtn;  
    2.   
    3. import android.content.Context;  
    4. import android.graphics.Color;  
    5. import android.util.AttributeSet;  
    6. import android.view.LayoutInflater;  
    7. import android.view.MotionEvent;  
    8. import android.view.View;  
    9. import android.view.View.OnTouchListener;  
    10. import android.widget.ImageView;  
    11. import android.widget.LinearLayout;  
    12. import android.widget.TextView;  
    13.   
    14. public class MyImgBtn extends LinearLayout {  
    15.   
    16.     private ImageView mImgView = null;  
    17.     private TextView mTextView = null;  
    18.     private Context mContext;  
    19.   
    20.       
    21.     public MyImgBtn(Context context, AttributeSet attrs) {  
    22.         super(context, attrs);  
    23.         // TODO Auto-generated constructor stub  
    24.         LayoutInflater.from(context).inflate(R.layout.myimgbtn_layout, this, true);  
    25.         mContext = context;  
    26.         mImgView = (ImageView)findViewById(R.id.img);  
    27.         mTextView = (TextView)findViewById(R.id.text);  
    28.                   
    29.           
    30.     }  
    31.   
    32.   
    33.   
    34.    /*设置图片接口*/  
    35.     public void setImageResource(int resId){  
    36.         mImgView.setImageResource(resId);  
    37.     }  
    38.       
    39.     /*设置文字接口*/  
    40.     public void setText(String str){  
    41.         mTextView.setText(str);  
    42.     }  
    43.     /*设置文字大小*/  
    44.     public void setTextSize(float size){  
    45.         mTextView.setTextSize(size);  
    46.     }  
    47.   
    48.   
    49.   
    50.   
    51. //     /*设置触摸接口*/  
    52. //    public void setOnTouch(OnTouchListener listen){  
    53. //        mImgView.setOnTouchListener(listen);  
    54. //        //mTextView.setOnTouchListener(listen);  
    55. //    }  
    56.   
    57. }  


    第三部分,主布局main.xml:

    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.   
    8.     <TextView  
    9.         android:layout_width="fill_parent"  
    10.         android:layout_height="wrap_content"  
    11.         android:text="@string/hello" />  
    12.     <yan.guoqi.testimgbtn.MyImgBtn  
    13.         android:id="@+id/MyIBtn_1"  
    14.         android:layout_width="wrap_content"  
    15.         android:layout_height="wrap_content"  
    16.         android:layout_gravity="center_horizontal"          
    17.         android:clickable="true"  
    18.         android:focusable="true"        
    19.         />  
    20.   
    21. </LinearLayout>  

    第四部分,主程序:

    1. package yan.guoqi.testimgbtn;  
    2.   
    3. import android.app.Activity;  
    4. import android.os.Bundle;  
    5. import android.view.View;  
    6. import android.view.View.OnClickListener;  
    7. import android.widget.Toast;  
    8.   
    9. public class TestImgBtnActivity extends Activity {  
    10.      
    11.     private MyImgBtn MyIBtn1 = null;  
    12.       
    13.     /** Called when the activity is first created. */  
    14.     @Override  
    15.     public void onCreate(Bundle savedInstanceState) {  
    16.         super.onCreate(savedInstanceState);  
    17.         setContentView(R.layout.main);  
    18.           
    19.         MyIBtn1 = (MyImgBtn)findViewById(R.id.MyIBtn_1);  
    20.         MyIBtn1.setImageResource(R.drawable.ic_launcher);  
    21.         MyIBtn1.setText("欢迎光临");  
    22.         MyIBtn1.setTextSize(24.0f);  
    23.         //MyIBtn1.setOnTouch(new MyOnTouchListener());  
    24.         MyIBtn1.setOnClickListener(new OnClickListener() {  
    25.               
    26.             public void onClick(View arg0) {  
    27.                 // TODO Auto-generated method stub  
    28.                 Toast.makeText(TestImgBtnActivity.this,  
    29.                                "您好",  
    30.                                Toast.LENGTH_SHORT)  
    31.                                .show();  
    32.                   
    33.             }  
    34.         });  
    35.     }  
    36. }  


    这种方法很直观简单,与之第一种用Gallery方法而言更容易理解。就是自定义一个类,第一种方法虽然不用自定义类,但是Gallery相关的适配器配置和那个View相关的如果第一次会不大习惯。这种效果也不错,图就不贴了。尤其适合做那种背景是纯色,里面嵌套图片+文字。就是360手机安全卫士的主窗口,大家可以看下。应该就是为这种方式做的。美中不足的是,360手机安全卫士的主窗口里,你点击一下,背景会变。也就是说这还缺少个onTouchListener,稍后我补上。

  • 相关阅读:
    *** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory
    linker command failed with exit code 1 (use -v to see invocation)
    谈谈我对PhoneGap的看法——(摘自唐巧的技术博客)
    ssh key一键自动化生成公钥私钥,并自动分发上百服务器免密码交互
    2018年Linux运维人员必会开源运维工具体系总结
    CentOS 6下PXE+Kickstart无人值守安装操作系统
    IOS优秀博客
    Nginx简单实现网站的负载均衡
    堆排序 Heap Sort
    h5移动端设计页面
  • 原文地址:https://www.cnblogs.com/tc310/p/4523473.html
Copyright © 2011-2022 走看看