在应用程序中,我可以简单地将 attr.xml 定义为 values 并将其用于设计布局。但是我不能将这个属性编程设置到小部件中,例如我有这个属性:
<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 程序,我想使用定义到 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
我不能像设置 times font 那样将枚举设置为小部件
如何通过在 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, 0, 8, 0);
Button button = new Button(context);
if (buttonTheme != -1) {
button.setBackgroundResource(buttonTheme);
}
button.setPadding(0,2,0,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);
}
最佳答案
也许这篇文章能帮到你Android: Set Custom Attributes Programmatically and Via XML .请注意,应该在某处(例如在“AppTheme”中)为 R.attr.font_button 属性指定一个样式。因为它只是一个属性,而且它持有对值的引用。
如果您在“AppTheme”中指定了样式:
ButtonTextStyle button = (TextView) findViewById(R.id.btn_way_bill_click);
Resources.Theme themes = getTheme();
TypedValue storedValueInTheme = new TypedValue();
if (themes.resolveAttribute(R.attr.font_button, storedValueInTheme, true))
button.setTextAppearance(storedValueInTheme.data);
或者如果您声明了自己的主题。可以声明如下
<resources>
<style name="ConsolasBtn" parent="@android:style/Widget">
<item name="font_button">consolas</item>
</style>
</resources>
然后您需要将这个主题应用到您的按钮上。尝试
ButtonTextStyle button = (TextView) findViewById(R.id.btn_way_bill_click);
button.setTextAppearance(this, R.style.ConsolasBtn); // *this* refers to Context
或者尝试将 R.style.ConsolasBtn 作为 defStyle 传递给构造函数 ButtonTextStyle(Context context, AttributeSet attrs, int defStyle)争论。
另见 Styles and Themes , Resources.Theme , TypedValue , attrs .
关于用于 xml 小部件的 Android 编程设置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29691479/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog