加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml小部件的Android程序集属性

发布时间:2020-12-16 05:33:31 所属栏目:百科 来源:网络整理
导读:在应用程序中,我可以简单地将attr.xml定义为值并将其用于设计布局.但我无法将此属性编程设置为小部件,例如我有这个属性: declare-styleable name="ButtonTextStyle" attr name="font_button" enum name="consolas" value="0" / enum name="times" value="1"
在应用程序中,我可以简单地将attr.xml定义为值并将其用于设计布局.但我无法将此属性编程设置为小部件,例如我有这个属性:
<declare-styleable name="ButtonTextStyle">
    <attr name="font_button">
        <enum name="consolas"      value="0" />
        <enum name="times"    value="1" />
    </attr>
</declare-styleable>

我可以通过以下方式将此属性用于xml:

<com.sample.app.Widgets.ButtonTextStyle
    android:id="@+id/btn_way_bill_click"
    android:layout_width="fill_parent"
    app:font_button="consolas">

现在我将一些Button programical定义为项目,我想使用定义到font_button中的consolas字体:

button.setTextAppearance(context,R.attr.font_button);

但我得到这个代码的错误,我无法解决问题,我从Button定义自定义类扩展,如下所示:

public class ButtonTextStyle extends Button {
    private Context mContext;
    public ButtonTextStyle(Context context,AttributeSet attrs,int defStyle) {
        super(context,attrs,defStyle);
        mContext = context;
        init(attrs,defStyle);
    }

    public ButtonTextStyle(Context context,AttributeSet attrs) {
        super(context,attrs);
        mContext = context;
        init(attrs,-1);
    }

    public ButtonTextStyle(Context context) {
        super(context);
        mContext = context;
        init(null,-1);
    }

    private void init(AttributeSet attrs,int defStyle) {
        if (!isInEditMode()) {
            TypedArray a = mContext.obtainStyledAttributes(attrs,R.styleable.ButtonTextStyle,defStyle,0);
            String str = a.getString(R.styleable.ButtonTextStyle_font_button);
            switch (Integer.parseInt(str)) {
                case 0:
                    str = "fonts/consolas.ttf";
                    break;
                case 1:
                    str = "fonts/times.ttf";
                    break;
            }
            setTypeface(FontManager.getInstance(getContext()).loadFont(str));
        }
    }
}

问题:

Expected resource of type style

并且我不能将例如enum设置为与set times字体一样的小部件

如何设置这个自定义类的程序,其属性定义为attr.xml

更新:
我的类添加按钮是下面的方法.我想以编程方式为其设置字体:

private void addButton(final CDialog owner,final Dialog dialog,final DialogButton dlgBtn) {
    LinearLayout linearLayout = (LinearLayout) dialog.findViewById(R.id.tsw__layerButton);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,70);
    //layoutParams.setMargins(11,8,0);

    Button button = new Button(context);
    if (buttonTheme != -1) {
        button.setBackgroundResource(buttonTheme);
    }
    button.setPadding(0,2,0);
    button.setGravity(Gravity.CENTER);
    button.setText(buttonMsg[dlgBtn.ordinal()]);
    button.setTextSize(14);
    button.setTextColor(G.context.getResources().getColor(R.color.white_text));
    button.setWidth(dpToPx(buttonWidth));
    //button.setHeight(dpToPx(32));
    button.setLayoutParams(layoutParams);

    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
            if (dialogListener != null) {
                dialogListener.onCloseDialog(owner,dlgBtn);
            }
        }
    });
    linearLayout.addView(button);
}
我相信实现你想要做的事情的正确方法是首先声明一个枚举属性……
<declare-styleable name="ButtonTextStyle">
    <attr name="font_button" format="enum"> 
        <enum name="consolas"      value="0" />
        <enum name="times"    value="1" />
    </attr>
</declare-styleable>

只有原始代码更改才是format属性.然后你可以声明这些自定义样式属性如下…

<com.sample.app.Widgets.ButtonTextStyle
    android:id="@+id/btn_way_bill_click"
    android:layout_width="fill_parent"
    app:font_button="consolas">

但是,不要忘记在父布局中声明子窗口小部件所属的命名空间,否则您将无法引用应用程序别名

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res/com.sample.app.Widgets">

最后,你的init方法看起来像这样……

private void init(AttributeSet attrs,int defStyle) {
        if (!isInEditMode()) {
            int i = -1;
            TypedArray a = mContext.obtainStyledAttributes(attrs,0);

            try{
                a.getInt(R.styleable.ButtonTextStyle_font_button,-1);
            }
            finally{
               a.recycle();
            }

            switch (i) {
                case 0:
                    str = "fonts/consolas.ttf";
                    break;
                case 1:
                    str = "fonts/times.ttf";
                    break;
            }

            setTypeface(FontManager.getInstance(getContext()).loadFont(str));
        }
    }

以编程方式设置属性

要以编程方式设置该属性,您需要公开一个采用整数值的“setter”方法,然后根据该值更改类型face.为了让您的自定义小部件的使用者更容易,您还可以在类中定义两个常量…

public static final int CONSOLAS = 0;
public static final int TIMES = 1;

然后定义setter方法……

public void setFontButton(int fontButton) {
        if (fontButton != CONSOLAS && fontButton != TIMES) {
            throw new IllegalArgumentException(
                    "fontButton must be one of CONSOLAS or TIMES");
        }

        //load the font and set it here,the same way you did in "init"
    }

你所要做的就是调用setter方法并传入一个指定的常量…

buttonTextStyle1.setFontButton(ButtonTextStyle.CONSOLAS);

然而另一个更新

而不是……

Button button = new Button(context);

做…

ButtonTextStyle button = new ButtonTextStyle(context);

并确保导入正确的类,以便它可以在代码中使用…

import com.sample.app.Widgets.ButtonTextStyle;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读