andorid 自定义控件(一)组合控件
我们在实际项目开发中,必然会遇到已有控件无法满足的功能需求,这就需要我们开发人员自定义实现。很多初学者一听到这些都比较犯怵。其实真正掌握了原理步骤,一切都不叫事儿。如果想进阶为android高手,自定义控件必须逾越,扫清这些障碍。当做一种乐趣吧。下面开始着手讲解。
1.自定义控件常见的几种形式:
(1)自定义扩展已有控件(比如继承TextView,Button,ViewPager等,常见的比如解决Viewpager的滑动冲突等)
(2)组合控件使用(常见如设置里的经常看见左边按钮,中间文字,右边箭头的样式,完全就可以组合实现,减少繁琐的重复layout布局)
(3)完全自定义View,View Group(这个才是大餐)等。
咱们主要讲解组合控件以及高逼格的完全自定义。兄弟们,莫急,莫急,咱们一步一步的,先来个简单的组合控件吧,先把操作流程会了。就跟自学ios似的,先把整体需求分析,开发,上线,开发者账号等流程搞明白了,再深入研究技术一样,有了方向一些都是:那都不叫事。
咱们先从组合控件入手吧,最基本。我计划这个今天讲完。完全自定义的view这个内容延伸会很广(view的坐标,事件),可能3,4个篇幅都不见得讲完,所以先入门吧,请看下面分解。
2.组合控件的开发步骤,也是所有自定义开发的整体步骤:
(1)在xml(styles中)中定义控件的属性
1 <declare-styleable name="HorizonItemLayout"> 2 <attr name="imore_left_icon" format="reference|integer" /> 3 <attr name="imore_text" format="string" /> 4 <attr name="imore_right_icon" format="reference|integer" /> 5 </declare-styleable>
切记:1.styles里的atrr name=“”,括号里的名字不能重复。
2.format有各种类型,也可以有枚举的。比如
1 <attr name="imageViewPosition" format="enum"> 2 <enum name="left" value="0"/> 3 <enum name="right" value="1"/> 4 </attr>
(2)创建自定义类,我是以布局文件的为例。
1 public class HorizonRelativeView extends RelativeLayout { 2 3 public ImageView iv_left, iv_right; 4 5 public TextView tv; 6 7 public HorizonRelativeView(Context context) { 8 super(context); 9 } 10 11 public HorizonRelativeView(Context context, AttributeSet attrs) { 12 super(context, attrs); 13 View view = LayoutInflater.from(context).inflate(R.layout.imore_item_horizonrelativevie, this, true); 14 iv_left = (ImageView) view.findViewById(R.id.imore_left_icon); 15 iv_right = (ImageView) view.findViewById(R.id.imore_right_icon); 16 tv = (TextView) view.findViewById(R.id.imore_text); 17 TypedArray mArray = context.obtainStyledAttributes(attrs, R.styleable.HorizonItemLayout); 18 CharSequence text = mArray.getText(R.styleable.HorizonItemLayout_imore_text); 19 if (!(TextUtils.isEmpty(text))) { 20 tv.setText(text); 21 } 22 23 Drawable left_icon = mArray.getDrawable(R.styleable.HorizonItemLayout_imore_left_icon); 24 if (left_icon != null) { 25 iv_left.setImageDrawable(left_icon); 26 }else{ 27 iv_left.setVisibility(View.GONE); 28 } 29 30 Drawable right_icon = mArray.getDrawable(R.styleable.HorizonItemLayout_imore_right_icon); 31 if (right_icon != null) { 32 iv_right.setImageDrawable(right_icon); 33 }else{ 34 iv_right.setVisibility(View.GONE); 35 } 36 mArray.recycle(); 37 } 38 39 public void setLeftImageResource(int rid) { 40 iv_left.setImageResource(rid); 41 } 42 43 public void setRightImageResource(int rid) { 44 iv_right.setImageResource(rid); 45 } 46 47 public void setText(String text) { 48 if (!TextUtils.isEmpty(text)) { 49 tv.setText(text); 50 } 51 } 52 }
(3)在layout中加入自定义控件
1 <LinearLayout 2 android:layout_width="match_parent" 3 android:layout_height="fill_parent" 4 android:background="@color/white" 5 android:orientation="vertical" 6 android:layout_marginBottom="16dp" 7 android:paddingBottom="10dp" 8 android:paddingTop="10dp"> 9 10 <com.ui.widget.HorizonRelativeView 11 android:id="@+id/setup_recomment_rl" 12 android:layout_width="fill_parent" 13 android:layout_height="wrap_content" 14 zsapp:imore_right_icon="@drawable/arrow2" 15 zsapp:imore_text="推荐给朋友" 16 android:visibility="gone" 17 18 /> 19 <com.ui.widget.HorizonRelativeView 20 android:id="@+id/setup_pinglun_rl" 21 android:layout_width="fill_parent" 22 android:layout_height="wrap_content" 23 zsapp:imore_right_icon="@drawable/arrow2" 24 zsapp:imore_text="喜欢就去评一下" 25 26 /> 27 <com.ui.widget.HorizonRelativeView 28 android:id="@+id/setup_feedback_rl" 29 android:layout_width="fill_parent" 30 android:layout_height="wrap_content" 31 zsapp:imore_right_icon="@drawable/arrow2" 32 zsapp:imore_text="意见反馈" 33 34 /> 35 36 </LinearLayout>
注意在根标签里添加
xmlns:tools="http://schemas.android.com/tools"
xmlns:zsapp="http://schemas.android.com/apk/res-auto"
这样这个组合控件在任何遇到这种布局的layout都可以使用,当然你自己也可以进一步扩展比如left_icon的隐藏与否,字体大小,以及事件操作等等,暴露出接口接口即可,建议设计外边以及事件的都进行暴露接口。
这样一个没有任何重新布局(onLayout),重绘的(onDraw)的自定义组合控件就实现了。
下一篇重点讲解完全自定义控件。
posted on 2016-06-14 14:00 张顺(java-wing) 阅读(676) 评论(0) 编辑 收藏 举报