Exemplo n.º 1
0
 public static int getType(TypedArray array, int index) {
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) return array.getType(index);
   else {
     TypedValue value = array.peekValue(index);
     return value == null ? TypedValue.TYPE_NULL : value.type;
   }
 }
  /**
   * 介绍TypedArray使用方法 获取 attr中formate 十种类型属性值
   * float,integer,boolean,fraction,string,dimension,color,reference,enum,flag
   *
   * <p>
   *
   * @param context 上下文环境
   * @param attrs 属性集合
   */
  private void printAttributes(Context context, AttributeSet attrs) {
    TypedArray typedArray =
        context.obtainStyledAttributes(attrs, R.styleable.CustomTextViewStyle, 0, 0);
    System.out.println(
        "typedArray.getChangingConfigurations() = "
            + Integer.toHexString(typedArray.getChangingConfigurations()));
    float float_value = typedArray.getFloat(R.styleable.CustomTextViewStyle_float_value, 0f);
    int integer_value = typedArray.getInteger(R.styleable.CustomTextViewStyle_integer_value, 0);
    boolean boolean_value =
        typedArray.getBoolean(R.styleable.CustomTextViewStyle_boolean_value, false);

    // public float getFraction (int index, int base, int pbase, float defValue)
    // 如果值为10% 则 fraction_value=10%*base
    // 如果值格式为10%p,则fraction_value=10%*pbase
    float fraction_value =
        typedArray.getFraction(R.styleable.CustomTextViewStyle_fraction_value, 1, 1, 0);
    String string_value = typedArray.getString(R.styleable.CustomTextViewStyle_string_value);

    // 获取像素值,浮点数  eg:27.5625
    float dimension_value_float =
        typedArray.getDimension(R.styleable.CustomTextViewStyle_dimension_value, 0f);
    // 将取得浮点像素值四舍五入 eg:28
    int dimension_value =
        typedArray.getDimensionPixelSize(R.styleable.CustomTextViewStyle_dimension_value, 0);
    // 将取得浮点像素值直接截取整数部分 eg:27
    int dimension_value_offset_ =
        typedArray.getDimensionPixelOffset(R.styleable.CustomTextViewStyle_dimension_value, 0);

    int color_value = typedArray.getColor(R.styleable.CustomTextViewStyle_color_value, 0);
    int reference_drawable_value =
        typedArray.getResourceId(R.styleable.CustomTextViewStyle_reference_drawable_value, 0);
    int reference_array_value =
        typedArray.getResourceId(R.styleable.CustomTextViewStyle_reference_array_value, 0);
    int enum_value = typedArray.getInt(R.styleable.CustomTextViewStyle_enum_value, -1);
    int flag_value = typedArray.getInt(R.styleable.CustomTextViewStyle_flag_value, -1);

    System.out.println(
        "float_value = ["
            + float_value
            + "], integer_value = ["
            + integer_value
            + "], "
            + "boolean_value = ["
            + boolean_value
            + "], fraction_value = ["
            + fraction_value
            + "], string_value = ["
            + string_value
            + "], dimension_value = ["
            + dimension_value
            + "], color_value = ["
            + color_value
            + "], "
            + "reference_drawable_value ="
            + " [0x"
            + Integer.toHexString(reference_drawable_value)
            + "], enum_value = ["
            + enum_value
            + "], "
            + "flag_value1 = ["
            + flag_value
            + "]");

    // 后期数据处理,设置左边图片
    Drawable drawable;
    drawable = typedArray.getDrawable(R.styleable.CustomTextViewStyle_reference_drawable_value);
    // or
    drawable = context.getDrawable(reference_drawable_value);
    drawable.setBounds(new Rect(0, 0, 50, 50));
    setCompoundDrawables(drawable, null, null, null);
    // 设置文字是否大写,斜体
    if (flag_value >= 0) {
      Typeface typeface = getTypeface();
      setTypeface(Typeface.defaultFromStyle(flag_value));
    }

    // 其他方法getTextArray
    CharSequence[] arrays;
    arrays = typedArray.getTextArray(R.styleable.CustomTextViewStyle_reference_array_value);
    // or
    arrays = context.getResources().getTextArray(reference_array_value);
    for (int i = 0; i < arrays.length; i++) {
      System.out.println("arrays[" + i + "] = " + arrays[i]);
    }

    // 遍历TypedArray
    for (int i = 0, m = typedArray.getIndexCount(); i < m; i++) {
      System.out.println(
          "typedArray"
              + i
              + " type= "
              + typedArray.getType(i)
              + " value="
              + typedArray.getString(i));
    }

    int textsize = typedArray.getDimensionPixelSize(R.styleable.CustomTextViewStyle_text_size, 10);
    int textcolor = typedArray.getColor(R.styleable.CustomTextViewStyle_text_color, Color.BLACK);
    String text = typedArray.getString(R.styleable.CustomTextViewStyle_text_content);
    int padding = typedArray.getDimensionPixelSize(R.styleable.CustomTextViewStyle_padding, 0);

    setTextColor(textcolor);
    setTextSize(textsize);
    setText(text);
    setPadding(padding, padding, padding, padding);

    typedArray.recycle();
  }