美文网首页
Android自定义View基础知识

Android自定义View基础知识

作者: 天神Deity | 来源:发表于2018-01-25 23:24 被阅读16次

一.declare-styleable 中的 format 枚举

属性名 属性说明 使用示例
reference 引用某一个资源Id app:custParams="@drawable/id"
color 颜色 app:custParams="@color/id"
boolean 布尔值 app:custParams="true"
dimension 尺寸 app:custParams="20dp"
float 浮点值 app:custParams="0.7"
integer 整型 app:custParams="10"
string 字符串 app:custParams="自定义属性"
fraction 百分比 app:custParams="50%"
enum 枚举值 app:custParams="Vertical"
flag 位或运算 app:custParams="LEFT/TOP"

一般来说,属性声明方式为:

<!--其中type可以是上面10中类型中的任何一种或者组合-->
<attr name="custParams" format="type" />

enum、flag的声明比较特殊

<!--enum 声明-->
<attr name="custParams"  >
      <enum name="vertical" value = "0" />
      <enum name="hort" value = "1" />
</attr>
<!--flag 声明-->
<attr name="custParams"  >
      <flag name="flag1" value = "1" />
      <flag name="flag2" value = "2" />
</attr>

二 自定义View的四个构造函数
在自定义View时,分别有不同参数的4个构造函数,它们分别被谁调用的?

  /**
   * 最简单的构造函数
   * Simple constructor to use when creating a view from code.
   * @param context The Context the view is running in, 
   * through which it can access the current theme, resources, etc.
   */
   public View(Context context)

参数只有上下文的构造函数,一般使用系统默认主题及属性,该方法通常由java代码生成一个控件实例时调用该构造方法,例如以下的使用方式:

ImageView image = new ImageView(MainActivity.this)
   /**
     * Constructor that is called when inflating a view from XML. This is called
     * when a view is being constructed from an XML file, supplying attributes
     * that were specified in the XML file. This version uses a default style of
     * 0, so the only attribute values applied are those in the Context's Theme
     * and the given AttributeSet.
     *
     * <p>
     * The method onFinishInflate() will be called after all children have been
     * added.
     *
     * @param context The Context the view is running in, through which it can
     *        access the current theme, resources, etc.
     * @param attrs The attributes of the XML tag that is inflating the view.
     * @see #View(Context, AttributeSet, int)
     */
    public View(Context context, @Nullable AttributeSet attrs)

当从XML文件构造视图时该构造方法被调用,提供在XML文件中指定的属性。 此版本使用默认样式为0,因此所应用的属性值是Context's Theme和给定AttributeSet中的属性值。

    /**
     * Perform inflation from XML and apply a class-specific base style from a
     * theme attribute. This constructor of View allows subclasses to use their
     * own base style when they are inflating. For example, a Button class's
     * constructor would call this version of the super class constructor and
     * supply <code>R.attr.buttonStyle</code> for <var>defStyleAttr</var>; this
     * allows the theme's button style to modify all of the base view attributes
     * (in particular its background) as well as the Button class's attributes.
     *
     * @param context The Context the view is running in, through which it can
     *        access the current theme, resources, etc.
     * @param attrs The attributes of the XML tag that is inflating the view.
     * @param defStyleAttr An attribute in the current theme that contains a
     *        reference to a style resource that supplies default values for
     *        the view. Can be 0 to not look for defaults.
     * @see #View(Context, AttributeSet)
     */
    public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr)

View(Context context, @Nullable AttributeSet attrs) 使用的是系统自带的主题,也就是当我们在xml中添加View控件,如果不设置View的相关属性值,那么这个View控件会中系统的默认主题中获取对应的属性值.如果你需要为该view自定主题,那么这个构造函数就是你想要的.

    /**
     * Perform inflation from XML and apply a class-specific base style from a
     * theme attribute or style resource. This constructor of View allows
     * subclasses to use their own base style when they are inflating.
     * <p>
     * When determining the final value of a particular attribute, there are
     * four inputs that come into play:
     * <ol>
     * <li>Any attribute values in the given AttributeSet.
     * <li>The style resource specified in the AttributeSet (named "style").
     * <li>The default style specified by <var>defStyleAttr</var>.
     * <li>The default style specified by <var>defStyleRes</var>.
     * <li>The base values in this theme.
     * </ol>
     * <p>
     * Each of these inputs is considered in-order, with the first listed taking
     * precedence over the following ones. In other words, if in the
     * AttributeSet you have supplied <code>&lt;Button * textColor="#ff000000"&gt;
     * </code>
     * , then the button's text will <em>always</em> be black, regardless of
     * what is specified in any of the styles.
     *
     * @param context The Context the view is running in, through which it can
     *        access the current theme, resources, etc.
     * @param attrs The attributes of the XML tag that is inflating the view.
     * @param defStyleAttr An attribute in the current theme that contains a
     *        reference to a style resource that supplies default values for
     *        the view. Can be 0 to not look for defaults.
     * @param defStyleRes A resource identifier of a style resource that
     *        supplies default values for the view, used only if
     *        defStyleAttr is 0 or can not be found in the theme. Can be 0
     *        to not look for defaults.
     * @see #View(Context, AttributeSet, int)
     */
    public View(Context context, @Nullable AttributeSet attrs,
 int defStyleAttr, int defStyleRes)

参考链接:
Android View 四个构造函数详解
Android中自定义样式与View的构造函数中的第三个参数defStyle的意义

相关文章

网友评论

      本文标题:Android自定义View基础知识

      本文链接:https://www.haomeiwen.com/subject/fmwbaxtx.html