Android自定义“图片+文字”控件实现方法之 --------个人最推荐的一种

来源:互联网 发布:mac 安装linux 编辑:程序博客网 时间:2024/04/25 15:43

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

第一部分:myimgbtn_layout.xml

[html] view plaincopy<?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>  
第二部分,与之布局相对应的MyImgBtn.java 文件:


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 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);                                  }           /*设置图片接口*/      public void setImageResource(int resId){          mImgView.setImageResource(resId);      }            /*设置文字接口*/      public void setText(String str){          mTextView.setText(str);      }      /*设置文字大小*/      public void setTextSize(float size){          mTextView.setTextSize(size);      }          //     /*设置触摸接口*/  //    public void setOnTouch(OnTouchListener listen){  //        mImgView.setOnTouchListener(listen);  //        //mTextView.setOnTouchListener(listen);  //    }    }  

第三部分,主布局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>  

第四部分,主程序:

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("欢迎光临");          MyIBtn1.setTextSize(24.0f);          //MyIBtn1.setOnTouch(new MyOnTouchListener());          MyIBtn1.setOnClickListener(new OnClickListener() {                            public void onClick(View arg0) {                  // TODO Auto-generated method stub                  Toast.makeText(TestImgBtnActivity.this,                                 "您好",                                 Toast.LENGTH_SHORT)                                 .show();                                }          });      }  }  

适合做那种背景是纯色,里面嵌套图片+文字。就是360手机安全卫士的主窗口,大家可以看下。应该就是为这种方式做的。美中不足的是,360手机安全卫士的主窗口里,你点击一下,背景会变

原创粉丝点击