当前位置: 移动技术网 > IT编程>移动开发>Android > Android中自定义一个View的方法详解

Android中自定义一个View的方法详解

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

缪月光,京本政树 京本大我,马尔西娅-爱子

本文实例讲述了android中自定义一个view的方法。分享给大家供大家参考,具体如下:

android中自定义view的实现比较简单,无非就是继承父类,然后重载方法,即便如此,在实际编码中难免会遇到一些坑,我把自己遇到的一些问题和解决方法总结一下,希望对广大码友们有所帮助。

注意点① 用xml定义layout时,root element 最好使用merge

当我们需要继承一个布局比较复杂的viewgroup(比较多的是linearlayout、relativelayout)时,通常会用xml来写布局,然后在自定义的view类中inflate这个定义了layout的xml文件。

首先新建一个名为 mylayout 的 class 文件,在 init 方法中解析稍后定义的xml文件。

/**
 * created by liangfei on 4/14/15.
 */
public class mylayout extends linearlayout {
  public mylayout(context context) {
    super(context);
    init();
  }
  private void init() {
    setorientation(vertical);
    view rootview = inflate(getcontext(), r.layout.my_layout, this);
    ((textview) rootview.findviewbyid(r.id.title)).settext("mylayout");
    ((textview) rootview.findviewbyid(r.id.desc)).settext("a customized layout");
  }
}

然后新建一个取名为my_layout的布局文件, 并把 root element 设置成merge。

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
  <textview
    android:id="@+id/title"
    android:textsize="16sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
  <textview
    android:id="@+id/desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</merge>

用 android sdk 附带的 monitor 工具查看一下运行时的布局信息。

最顶层是一个framelayout,然后是一个linearlayout,里面有两个textview,可以看出布局没有冗余。

但是,如果把 root element 换成 linearlayout,效果会怎么样呢?

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <textview
    android:id="@+id/title"
    android:textsize="16sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
  <textview
    android:id="@+id/desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</linearlayout>

很明显,用 linearlayout 做 root element 后,布局多了一个层级,成了影响性能的一个因素。

注意点② 重载子类构造函数时要弄清楚父类做了哪些操作

先从我一个惨痛的教训开始,当时我这样自定义了一个button:

/**
 * created by liangfei on 4/14/15.
 */
public class mybutton extends button {
  public mybutton(context context) {
    this(context, null);
  }
  public mybutton(context context, attributeset attrs) {
    this(context, attrs, 0);
  }
  public mybutton(context context, attributeset attrs, int defstyleattr) {
    super(context, attrs, defstyleattr);
    init();
  }
}

乍一看貌似没什么问题,构造函数的调用方式都是正确的,但是无论我怎么修改 mybutton 的属性,显示方式就是不正确。

其实问题就出在button类在构造函数中使用了一个defstyleattr, 而我这种写法会忽略掉这个defstyleattr - com.android.internal.r.attr.buttonstyle,稍读源码就知道了。

@remoteview
public class button extends textview {
  public button(context context) {
    this(context, null);
  }
  public button(context context, attributeset attrs) {
    this(context, attrs, com.android.internal.r.attr.buttonstyle);
  }
  public button(context context, attributeset attrs, int defstyleattr) {
    this(context, attrs, defstyleattr, 0);
  }
  public button(context context, attributeset attrs, int defstyleattr, int defstyleres) {
    super(context, attrs, defstyleattr, defstyleres);
  }
}

后来写代码的时候,我都是看了父类的源码之后才敢这么写,如果不确定就老老实实地写成下面这种形式。

/**
 * created by liangfei on 4/14/15.
 */
public class mybutton extends button {
  public mybutton(context context) {
    super(context);
    init();
  }
  public mybutton(context context, attributeset attrs) {
    super(context, attrs);
    init();
  }
  public mybutton(context context, attributeset attrs, int defstyleattr) {
    super(context, attrs, defstyleattr);
    init();
  }
}

其实,还有很多其他的坑,比如 button 的高度,后面抽时间再总结一下

更多关于android相关内容感兴趣的读者可查看本站专题:《android视图view技巧总结》、《android操作xml数据技巧总结》、《android编程之activity操作技巧总结》、《android资源操作技巧汇总》、《android文件操作技巧汇总》、《android操作sqlite数据库技巧总结》、《android操作json格式数据技巧总结》、《android数据库操作技巧总结》、《android编程开发之sd卡操作方法汇总》、《android开发入门与进阶教程》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

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

相关文章:

验证码:
移动技术网