Android 自定义小部件中使用的自定义XML属性

Android 自定义小部件中使用的自定义XML属性,android,custom-component,custom-attribute,Android,Custom Component,Custom Attribute,我正在创建各种Android小部件的子类来创建我自己的小部件。以下是我迄今为止所做的工作: (在my res/values/attr.xml中定义) 现在我想读取在XML代码中设置的枚举值。我怎么读呢?然后,根据提供的字体,我想设置我的自定义字体。任何帮助都将不胜感激。这正是我所需要的 <?xml version="1.0" encoding="utf-8"?> <resources> <attr name="font"> <enum nam

我正在创建各种Android小部件的子类来创建我自己的小部件。以下是我迄今为止所做的工作:

(在my res/values/attr.xml中定义)


现在我想读取在XML代码中设置的枚举值。我怎么读呢?然后,根据提供的字体,我想设置我的自定义字体。任何帮助都将不胜感激。

这正是我所需要的

<?xml version="1.0" encoding="utf-8"?>
<resources>

<attr name="font">
    <enum name="ARIAL_BOLD" value="1" />
    <enum name="ARIAL_ROUND_MT" value="2" />
    <enum name="HELVETICA" value="3" />
    <enum name="HELVETICA_BOLD" value="4" />
    <enum name="GILSANCE_LIGHT" value="5" />
</attr>

<declare-styleable name="CustomEditText">
    <attr name="font" />
</declare-styleable>
<declare-styleable name="CustomButton">
    <attr name="font" />
</declare-styleable>
<declare-styleable name="CustomTextView">
    <attr name="font" />
</declare-styleable>

</resources>

然后是我的自定义文件

public class CustomEditText extends android.widget.EditText {

    public CustomEditText(Context context) {
        super(context);
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.CustomEditText);
        final int fontValue = a.getInt(R.styleable.CustomEditText_font, 0);
        setTypeFace(fontValue);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.CustomEditText);
        final int fontValue = a.getInt(R.styleable.CustomEditText_font, 0);
        setTypeFace(fontValue);
    }

    public void setTypeFace(int fontValue) {
        Typeface myTypeFace = Typeface.createFromAsset(this.getContext()
            .getAssets(), getApplicationFont(fontValue));
        this.setTypeface(myTypeFace);
    }
}


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login_background"
    android:orientation="vertical" xmlns:app="http://schemas.android.com/apk/res/com.mycomp.myproj">

    <TextView
        android:id="@+id/topBarText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#80000000"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/application_name"
        android:textColor="@color/white"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:textSize="35sp" />

     <com.myapp.ui.CustomEditText
        app:font="ARIAL_ROUND_MT"
        android:id="@+id/samPLE"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#80000000"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/application_name"
        android:textColor="@color/white"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:textSize="35sp" />
public类CustomEditText扩展了android.widget.EditText{
公共CustomEditText(上下文){
超级(上下文);
}
公共CustomEditText(上下文、属性集属性){
超级(上下文,attrs);
TypedArray a=上下文。获取样式属性(属性,
R.styleable.CustomEditText);
final int fontValue=a.getInt(R.styleable.CustomEditText_font,0);
设置字体(字体值);
}
公共CustomEditText(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
TypedArray a=上下文。获取样式属性(属性,
R.styleable.CustomEditText);
final int fontValue=a.getInt(R.styleable.CustomEditText_font,0);
设置字体(字体值);
}
公共void setTypeFace(int-fontValue){
Typeface myTypeFace=Typeface.createFromAsset(this.getContext())
.getAssets(),getApplicationFont(fontValue));
这个.setTypeface(myTypeFace);
}
}

这对我有用。请注意,我的自定义视图被放置在一个Android库项目中

这就是我的工作

<?xml version="1.0" encoding="utf-8"?>
<resources>

<attr name="font">
    <enum name="ARIAL_BOLD" value="1" />
    <enum name="ARIAL_ROUND_MT" value="2" />
    <enum name="HELVETICA" value="3" />
    <enum name="HELVETICA_BOLD" value="4" />
    <enum name="GILSANCE_LIGHT" value="5" />
</attr>

<declare-styleable name="CustomEditText">
    <attr name="font" />
</declare-styleable>
<declare-styleable name="CustomButton">
    <attr name="font" />
</declare-styleable>
<declare-styleable name="CustomTextView">
    <attr name="font" />
</declare-styleable>

</resources>

然后是我的自定义文件

public class CustomEditText extends android.widget.EditText {

    public CustomEditText(Context context) {
        super(context);
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.CustomEditText);
        final int fontValue = a.getInt(R.styleable.CustomEditText_font, 0);
        setTypeFace(fontValue);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.CustomEditText);
        final int fontValue = a.getInt(R.styleable.CustomEditText_font, 0);
        setTypeFace(fontValue);
    }

    public void setTypeFace(int fontValue) {
        Typeface myTypeFace = Typeface.createFromAsset(this.getContext()
            .getAssets(), getApplicationFont(fontValue));
        this.setTypeface(myTypeFace);
    }
}


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login_background"
    android:orientation="vertical" xmlns:app="http://schemas.android.com/apk/res/com.mycomp.myproj">

    <TextView
        android:id="@+id/topBarText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#80000000"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/application_name"
        android:textColor="@color/white"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:textSize="35sp" />

     <com.myapp.ui.CustomEditText
        app:font="ARIAL_ROUND_MT"
        android:id="@+id/samPLE"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#80000000"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/application_name"
        android:textColor="@color/white"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:textSize="35sp" />
public类CustomEditText扩展了android.widget.EditText{
公共CustomEditText(上下文){
超级(上下文);
}
公共CustomEditText(上下文、属性集属性){
超级(上下文,attrs);
TypedArray a=上下文。获取样式属性(属性,
R.styleable.CustomEditText);
final int fontValue=a.getInt(R.styleable.CustomEditText_font,0);
设置字体(字体值);
}
公共CustomEditText(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
TypedArray a=上下文。获取样式属性(属性,
R.styleable.CustomEditText);
final int fontValue=a.getInt(R.styleable.CustomEditText_font,0);
设置字体(字体值);
}
公共void setTypeFace(int-fontValue){
Typeface myTypeFace=Typeface.createFromAsset(this.getContext())
.getAssets(),getApplicationFont(fontValue));
这个.setTypeface(myTypeFace);
}
}

这对我有用。请注意,我的自定义视图被放置在一个Android库项目中

我试过了,但被卡住了。当我最终得到答案时,我和你们分享了它。没有造成任何伤害。我试过了,但被卡住了。当我最终得到答案时,我和你们分享了它。没有造成伤害。