当前位置: 移动技术网 > 移动技术>移动开发>Android > Android自定义控件的创建方法

Android自定义控件的创建方法

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

本文为大家分享了android创建自定义控件的具体代码,供大家参考,具体内容如下

1、仿iphone 的风格,在界面的顶部放置一个标题栏。

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 >

 <linearlayout
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:background="#2197db"
  android:orientation="horizontal"
  android:layout_alignparenttop="true"
  android:layout_alignparentleft="true"
  android:layout_alignparentstart="true">

  <button
   android:id="@+id/title_back"
   android:layout_width="90dp"
   android:layout_height="40dp"
   android:layout_gravity="center"
   android:layout_margin="5dp"
   android:text="返回"
   />

  <textview
   android:id="@+id/title_text"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:layout_weight="1"
   android:gravity="center"
   android:text="标题"
   android:textcolor="#fff"
   android:textsize="24sp"
   />
  <button
   android:id="@+id/title_edit"
   android:layout_width="90dp"
   android:layout_height="40dp"
   android:layout_gravity="center"
   android:layout_margin="5dp"
   android:text="确定"
   />

 </linearlayout>
</relativelayout>

标题栏布局已经编写完成,剩下的就是如何在程序中使用这个标题栏。

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/title" />
</linearlayout>
//我们只需要通过一行 include语句将标题栏布局引入进来就可以了。

然后在 mainactivity 中将系统自带的标题栏隐藏掉

public class mainactivity extends activity {
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
requestwindowfeature(window.feature_no_title);
setcontentview(r.layout.activity_main);
}
}

我们还是需要在每个活动中为这些控件单独编写一次事件注册的代码。比如说标题栏中的返回按钮,其实不管是在哪一个活动中,这个按钮的功能都是相同的,即销毁掉当前活动,这种情况最好是使用自定义控件的方式来解决。

新建自定义的标题栏控件:

public class titlelayout extends linearlayout {
public titlelayout(context context, attributeset attrs) {
super(context, attrs);
layoutinflater.from(context).inflate(r.layout.title, this);
}
}


我们重写了 linearlayout 中的带有两个参数的构造函数,在布局中引入 titlelayout控件就会调用这个构造函数。然后在构造函数中需要对标题栏布局进行动态加载,这就要借助 layoutinflater 来实现了。通过 layoutinflater 的 from()方法可以构建出一个 layoutinflater对象,然后调用 inflate()方法就可以动态加载一个布局文件,inflate()方法接收两个参数,第一个参数是要加载的布局文件的 id,这里我们传入 r.layout.title,第二个参数是给加载好的布局再添加一个父布局,这里我们想要指定为 titlelayout,于是直接传入this

在布局文件中添加这个自定义控件 

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.xxxxxx.titlelayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
></com.example.xxxxxx.titlelayout>
</linearlayout>

我们来尝试为标题栏中的按钮注册点击事件,修改 titlelayout中的代码

public class titlelayout extends linearlayout {
 public titlelayout(context context, attributeset attrs) {
  super(context, attrs);
  layoutinflater.from(context).inflate(r.layout.title, this);
  button titleback = (button) findviewbyid(r.id.title_back);
  button titleedit = (button) findviewbyid(r.id.title_edit);

  titleback.setonclicklistener(new onclicklistener() {
   @override
   public void onclick(view v) {
    ((activity) getcontext()).finish();
   }
  });

  titleedit.setonclicklistener(new onclicklistener() {
   public static final string tag = "";

   @override
   public void onclick(view v) {
    toast.maketext(getcontext(), "重新运行程序", toast.length_short).show();
    log.i(tag, "111 ");
   }
  });
 }

}

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网