自定义控件(二)
来源:互联网 发布:print is python 编辑:程序博客网 时间:2024/06/04 19:18
定义自定义控件时所用到的资源文件attrs此处不再示例怎么定义,若要查看请看《自定义控件(一)》
此自定义控件使用性也较高,可以被其他activity很容易的反复调用,下面来看一下实现代码:
调用时的xml代码:
<span style="font-size:14px;">xmlns:myapp="http://schemas.android.com/apk/res-auto"</span>
<span style="font-size:14px;"><costomview.MyOwnView android:id="@+id/my_title_view" android:layout_width="match_parent" android:layout_height="wrap_content" myapp:text_first="左一" myapp:text_two="中间" myapp:text_three="右一" /></span>
<span style="font-size:14px;"></span>
<pre name="code" class="java"><span style="font-size:14px;"></span>
<span style="font-size:14px;">/** 自定义控件实现及监听* */public class MyOwnView extends RelativeLayout implements View.OnClickListener{ private LayoutInflater mInflater; private OnClickListener mClickListener; private String firstTxt,twoTxt,threeTxt; public void setMyViewOnClickListener(OnClickListener l){ //activity中注册所调用的方法,接口采用系统定义接口(也可自定义一个接口) mClickListener = l; } public MyOwnView(Context context) { super(context); } public MyOwnView(Context context, AttributeSet attrs) { super(context, attrs); init(context,attrs); } public MyOwnView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context,attrs); } public void init(Context context, AttributeSet attrs){ mInflater = LayoutInflater.from(context); View v = mInflater.inflate(R.layout.own_view_layout,this,true); //联系上下文解析原始布局 TypedArray a = null; try { a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.MyOwnView,0,0); //获取新布局中引用自定义控件设置的值 firstTxt = a.getString(R.styleable.MyOwnView_text_first); twoTxt = a.getString(R.styleable.MyOwnView_text_two); threeTxt = a.getString(R.styleable.MyOwnView_text_three); }finally { a.recycle(); //回收 } if (firstTxt != null){ TextView first = (TextView) v.findViewById(R.id.own_first_txt); //设置值在原始布局中 first.setText(firstTxt); first.setOnClickListener(this); //此处采用系统接口注册 } if (twoTxt != null){ TextView two = (TextView) v.findViewById(R.id.own_two_txt); two.setText(twoTxt); } if (threeTxt != null){ TextView three = (TextView) v.findViewById(R.id.own_three_txt); three.setText(threeTxt); three.setOnClickListener(this); } } @Override public void onClick(View v) { if (mClickListener != null){ mClickListener.onClick(v); } }}</span>
<span style="font-size:14px;">activity中实现:</span>
<pre name="code" class="html"><span style="font-size:14px;">public class MyViewTestActivity extends Activity { private MyOwnView myOwnView ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.myview_layout); myOwnView = (MyOwnView) findViewById(R.id.my_title_view); myOwnView.setMyViewOnClickListener(new View.OnClickListener() { //实现监听 @Override public void onClick(View v) { switch (v.getId()){ case R.id.own_first_txt: finish(); break; case R.id.own_three_txt: Toast.makeText(MyViewTestActivity.this,"点击成功",Toast.LENGTH_SHORT).show(); break; } } }); }}</span>
设置所用到的原始布局,
<span style="font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="40dp" android:background="@android:color/holo_blue_light"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/own_first_txt" android:textSize="14sp" android:layout_marginLeft="10dp" android:padding="5dp" android:layout_centerVertical="true" android:background="@drawable/own_view_press_selector" android:textColor="@android:color/white" android:text="返回"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/own_two_txt" android:text="标题" android:layout_centerInParent="true" android:textColor="@android:color/white" android:textSize="20sp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/own_three_txt" android:textSize="14sp" android:layout_marginRight="10dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:textColor="@android:color/white" android:padding="5dp" android:background="@drawable/own_view_press_selector" android:text="设置"/></RelativeLayout></span>效果图如下:
0 0
- 自定义控件(二)
- 自定义控件(二)
- .net自定义控件(二)
- 自定义控件入门(二)
- 自定义控件View(二)
- Android 自定义控件(二)
- android 自定义控件(二)
- android 自定义控件(二)
- Android自定义控件(二)
- Android自定义控件(二)组合控件
- 自定义控件(二)--复合控件
- 自定义控件二(创建复合控件)
- 自定义控件(二)自定义Dialog
- 自定义asp.net控件分析(二)
- 自定义控件学习笔记(二)
- Windows Mobile 自定义控件(二)
- Windows Mobile 自定义控件(二)
- Windows Mobile 自定义控件(二)
- 【codevs1246】丑数,STL与取模大质数的好处
- Launcher3源码分析 — ui结构
- zzuoj10434: good string
- Codeforces Round #105 (Div. 2) 148C Terse princess(脑洞)
- android中常用菜单(menu)的基本知识
- 自定义控件(二)
- Cannot add or update a child row: a foreign key constraint fails (`mysql`.`ordermx`, CONSTRAINT `kf
- secureCRT使用rz和sz上传和下载
- 符号表文件小知识
- SpriteBuilder action最小持续时间
- 大文件内字符串排序
- Android studio --导入库文件
- iOS中如何用锁
- xcode 使用代码块Code Snippets