binbinyang---自定义ImagView---实现任意图片.上面添加文字
2015-10-27 11:27
411 查看
自定义ImagView---实现任意图片.上面添加文字
开发中. 会遇到各种自定义的控件,今天上午开发中遇到一个,发现遇到很多次了.所以这次就干脆记录下来.供大家一起学习成长
圆框里的 就是要自定义的控件
直接贴代码吧
这是继承布局的JAVA代码
这是主activity的布局代码
这是主代码..注释部分 可以根据你个人喜好来,添加对应的
开发中. 会遇到各种自定义的控件,今天上午开发中遇到一个,发现遇到很多次了.所以这次就干脆记录下来.供大家一起学习成长
圆框里的 就是要自定义的控件
直接贴代码吧
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" 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" android:src="@drawable/left_index_bg" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_alignRight="@+id/img" android:layout_marginTop="26dp" android:gravity="center" android:text="+8" /> </RelativeLayout>这是XML布局的代码文件
public class MyImgBtn extends RelativeLayout { 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.demo, 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); // } }
这是继承布局的JAVA代码
<?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:background="#ffffff" android:orientation="vertical" > <com.example.fragmentdemo.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>
这是主activity的布局代码
public class MainActivity 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.activity_main); MyIBtn1 = (MyImgBtn) findViewById(R.id.MyIBtn_1); //MyIBtn1.setImageResource(R.drawable.left_index_bg); // MyIBtn1.setText("+8"); // MyIBtn1.setTextSize(12.0f); // MyIBtn1.setOnTouch(new MyOnTouchListener()); MyIBtn1.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "您好", Toast.LENGTH_SHORT) .show(); } }); } }
这是主代码..注释部分 可以根据你个人喜好来,添加对应的
相关文章推荐
- android图片绘制
- L脚本语言图形接口
- 存储函数
- 快速查找UPX 的OEP的方法(一)
- levelDB源码在vs2010上编译全过程
- 1.21;http://www.baidu.com;v1.2版
- C# WinForm获取当前路径汇总
- 【转】linux 中fork()函数详解
- Apache禁止指定user_agent
- Android启动相机拍照并返回图片
- 【HDOJ】【1008】Elevator
- @jsonignore的使用
- PostgreSQL修改数据库表的列属性(ALTER语句)
- 2015软件测试视频教程
- OC --- NSString 对象的相关验证(Category)
- IOS--关于UIViewController添加UINavigationBar的详细实现方法
- Spark操作hbase
- [Linux] vim的高亮查找操作
- 调用系统的DownLoadManager 去下载资源
- Linux运维第三阶段(十)POSTFIX