如何从TypedArray获取标志属性?

用户名

我在android自定义textview中设置了多个标志属性,如何使用TypedArray恢复属性

<some.text.view.that.i.defined.TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        font:sanFrancisco="bold|italic"/>


<attr name="fontSanFrancisco">
    <flag name="regular" value="0"/>
    <flag name="bold" value="1"/>
    <flag name="italic" value="2"/>
</attr>

<declare-styleable name="font">
    <attr name="sanFrancisco" format="flag"/>
</declare-styleable>
头发

attrs.xml

  <resources>
    <declare-styleable name="font">
        <attr name="my_sampleFlag" />
    </declare-styleable>
    <attr name="my_sampleFlag">
        <flag name="regular" value="0" />
        <flag name="bold" value="1" />
        <flag name="italic" value="2" />
    </attr>
</resources>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:custom="http://schemas.android.com/apk/res-auto"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="center">
    <com.example.checkstack.MyView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:text="Hello World"
       android:layout_marginTop="20dip"/>
</LinearLayout>

我的看法

public class MyView extends TextView {

    public MyView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        applyCustomFont(context, attrs);
    }

    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        applyCustomFont(context, attrs);
    }

    private void applyCustomFont(Context context, AttributeSet attrs) {

          TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.font);
            int textStyle = a.getInt(R.styleable.font_my_sampleFlag, 1);
            setTypeface(null, textStyle);
             a.recycle();  
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用TypedArray获取Drawable(具有drawable别名)

来自分类Dev

Android动态获取TypedArray

来自分类Dev

如何从 OptionParser 中获取指定的选项标志

来自分类Dev

如何找到typedArray的类型?

来自分类Dev

在 TypeScript 中获取 TypedArray 的类型

来自分类Dev

GhostDoc 如何获取属性的属性?

来自分类Dev

如何修复HtmlService的“解析'sandbox'属性时出错”标志错误?

来自分类Dev

如何从对象获取属性?

来自分类Dev

如何随机获取属性

来自分类Dev

如何从对象获取属性?

来自分类Dev

如何获取对象的属性

来自分类Dev

如何从plist获取属性

来自分类Dev

如何获取属性值?

来自分类Dev

如何获取属性的值?

来自分类Dev

如何从已编译的类中获取Javac标志

来自分类Dev

如何获取OCaml链接器标志以与C ++ cmake构建链接

来自分类Dev

如何在我的网站上获取用户的国家标志

来自分类Dev

如何从控制器获取带有参数/标志的值

来自分类Dev

如何使用带标志的语言获取下拉当前值?

来自分类Dev

如何读取1个标志位并同时获取整数

来自分类Dev

替换属性中的标志

来自分类Dev

从网络接口获取标志

来自分类Dev

如何获取属性而不是收集属性

来自分类Dev

如何使用属性本身获取属性名称

来自分类Dev

如何使用Olingo获取EntityType的属性属性

来自分类Dev

如何使用属性本身获取属性名称

来自分类Dev

如何获取属性而不是集合属性

来自分类Dev

如何获取@对象属性的值?

来自分类Dev

如何获取属性的串联值?