当前位置: 移动技术网 > IT编程>移动开发>Android > Android自定义控件LinearLayout实例讲解

Android自定义控件LinearLayout实例讲解

2019年07月24日  | 移动技术网IT编程  | 我要评论

裘英俊相声,二把手txt,辉子爷vs顽皮哥

很多时候android常用的控件不能满足我们的需求,那么我们就需要自定义一个控件了。今天做了一个自定义控件的实例,来分享下。
首先定义一个layout实现按钮内部布局: 

<?xmlversion="1.0"encoding="utf-8"?>
 <linearlayoutxmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="horizontal">
 
 <imageview
 android:id="@+id/imageview1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_vertical"
 android:paddingbottom="5dip"
 android:paddingleft="40dip"
 android:paddingtop="5dip"
 android:src="@drawable/right_icon"/>
 
 <textview
 android:id="@+id/textview1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_vertical"
 android:layout_marginleft="8dip"
 android:text="确定"
 android:textcolor="#000000"/>
 
 </linearlayout>

接下来写一个类继承linearlayout,导入刚刚的布局,并且设置需要的方法,从而使的能在代码中控制这个自定义控件内容的显示。

 public class imagebtn extendslinearlayout {
 
 privateimageview imageview;
 privatetextview textview;
 
 publicimagebtn(context context) {
 super(context);
 // todo auto-generated constructor stub
 }
 publicimagebtn(context context, attributeset attrs) {
 super(context, attrs);
 // todo auto-generated constructor stub
 layoutinflater inflater=(layoutinflater) context.getsystemservice(context.layout_inflater_service);
 inflater.inflate(r.layout.imagebtn,this);
 imageview=(imageview) findviewbyid(r.id.imageview1);
 textview=(textview)findviewbyid(r.id.textview1);
 }
 
 /**
 * 设置图片资源
 */
 publicvoidsetimageresource(intresid) {
 imageview.setimageresource(resid);
 }
 
 /**
 * 设置显示的文字
 */
 publicvoidsettextviewtext(string text) {
 textview.settext(text);
 }
 
 }

在需要使用这个自定义控件的layout中加入这控件,只需要在xml中加入即可。

 <?xmlversion="1.0"encoding="utf-8"?>
 <linearlayoutxmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="horizontal">
 
 <cn.com.karl.view.imagebtn
 android:id="@+id/btn_right"
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:background="@drawable/btn"
 />
 
 <cn.com.karl.view.imagebtn
 android:id="@+id/btn_error"
 android:layout_marginleft="5dp"
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:background="@drawable/btn"
 />
 
 </linearlayout> 

这里用到了背景图片 在drawable/btn.xml

 <?xmlversion="1.0"encoding="utf-8"?>
 <selectorxmlns:android="http://schemas.android.com/apk/res/android">
 
 <itemandroid:state_focused="true"android:state_pressed="false"android:drawable="@drawable/btn_normal"></item>
 <itemandroid:state_pressed="true"android:drawable="@drawable/btn_white"></item>
 <itemandroid:state_checked="true"android:drawable="@drawable/btn_white"></item>
 <itemandroid:state_focused="false"android:state_pressed="false"android:drawable="@drawable/btn_normal"></item>
 
 </selector>

最后在activity中设置该控件,和其他控件差不多:

 public class identifybuttonactivity extendsactivity {
 privateimagebtn imagebtn1; 
 privateimagebtn imagebtn2;
 @override
 protectedvoidoncreate(bundle savedinstancestate) {
 // todo auto-generated method stub
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.identifybutton);
 
 imagebtn1=(imagebtn)this.findviewbyid(r.id.btn_right);
 imagebtn2=(imagebtn)this.findviewbyid(r.id.btn_error);
 imagebtn1.settextviewtext("确定");
 imagebtn2.settextviewtext("取消");
 imagebtn1.setimageresource(r.drawable.right_icon);
 imagebtn2.setimageresource(r.drawable.error_icon);
 
 imagebtn1.setonclicklistener(newview.onclicklistener() {
 
 publicvoidonclick(view v) {
 // todo auto-generated method stub
 toast.maketext(getapplicationcontext(),"点击的正确按钮",1).show();
 }
 });
 
 imagebtn2.setonclicklistener(newview.onclicklistener() {
 
 publicvoidonclick(view v) {
 // todo auto-generated method stub
 toast.maketext(getapplicationcontext(),"点击的错误按钮",1).show();
 }
 });
 }
 }

最后看看我们自定义控件的效果吧!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网