草庐IT

android - 库的样式属性没有值,即使它们已明确设置

coder 2023-11-25 原文

我制作了一个带有自定义 View 的库,该 View 在创建时会扩充布局。布局中的 View 使用 style="?attr/labelStyle" 或任何其他属性设置样式。

该属性在库的 attrs.xml 中声明:

<attr name="myViewStyle" format="reference"/>

<declare-styleable name="MyView">
    <attr name="labelStyle" format="reference|color"/>
</declare-styleable>

我已经在库的 styles.xml 中为此属性设置了默认值:

<style name="MyViewStyle">
    <item name="labelStyle">@style/LabelStyle</item>
</style>

<style name="LabelStyle">
    <item name="android:textColor">?android:attr/textColorPrimary</item>
    <item name="...">...</item>
</style>

最后在库的 themes.xml 中:

<style name="MyViewStyleLight" parent="Theme.AppCompat.Light">
    <item name="myViewStyle">@style/MyViewStyle</item>
</style>

现在这是库的默认样式,但它在主项目 styles.xml 中被覆盖了

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="myViewStyle">@style/MyViewStyleCustom</item>
</style>

<style name="MyViewStyleCustom" parent="MyViewStyleLight">
    <item name="android:textColor">@color/gray</item>
    <item name="...">...</item>
</style>

自定义 View 代码:

public MyView(Context context) {
    this(context, null, R.attr.myViewStyle, 0);
}

public MyView(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, R.attr.myViewStyle, 0);
}

public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    this(context, attrs, defStyleAttr, 0);
}

public MyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(createThemeWrapper(context, R.attr.myViewStyle, R.style.MyViewStyleLight),
            attrs, defStyleAttr, defStyleRes);
    initLayout();
}

private static Context createThemeWrapper(Context context, int styleAttr, int defaultStyle) {
    final TypedArray ta = context.obtainStyledAttributes(new int[]{styleAttr});
    int style = ta.getResourceId(0, defaultStyle);
    ta.recycle();
    return new ContextThemeWrapper(context, style);
}

private void initLayout() {
    LayoutInflater inflater = LayoutInflater.from(getContext());
    inflater.inflate(R.layout.my_view, this);
    ...
}

我在下面解释了 ContextThemeWrapper。现在应用程序在布局膨胀的那一行崩溃了。这是崩溃日志的重要部分:

android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class com.example.MyView
      at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
      [...]
    Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x7f030057 a=-1}
      at android.content.res.TypedArray.getDrawable(TypedArray.java:867)
      [...]

布局充气器无法找到属性的值。当我试图通过代码获取属性时,它什么也不返回。该属性实际上存在,只是它没有设置值,即使我已经明确设置了一个值。

我应该如何设计我的库的样式?我几乎可以肯定我所做的每件事都与 SublimePicker library 相同但它就是行不通。与 ContextThemeWrapper 的部分略有不同,但这可能不是问题所在。我觉得我忘记了某个地方的一个小东西,它使属性没有值(value),有些东西没有连接,我不知道。

我知道这是一个很长的问题,但是再简洁不过了,我尽可能地简化了一切。我更改了以前版本问题中的大部分信息,使其完全不同。这两个答案现在根本不相关,过去也不相关。赏金自动奖励。

如果这对某人有帮助,我可以将下载添加到我的实际项目中,但正如我所说,这个简化示例与我的项目具有完全相同的形式。

最佳答案

这个答案是基于我从你的问题和你与 Vinayak B 之间的对话中了解到的。如果我有误解,请纠正我。

应用程序和库中的 style.xml 存在差异。此外,我已经删除了 theme.xml 以及默认样式的 MyView.java 构造函数中的更改

我改变了以下事情

  • 在主项目styles.xml中覆盖

    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="myViewStyle">@style/MyViewStyleCustom</item>
    </style>
    
    <style name="MyViewStyleCustom" parent="MyViewStyle">
        <item name="labelStyle">@style/LabelStyle123</item>
    </style>
    
    <style name="LabelStyle123">
        <item name="android:textColor">#f00</item>
    </style>
    
  • 库样式.xml

    <resources>
        <style name="MyViewStyle">
            <item name="labelStyle">@style/LabelStyle</item>
            <item name="TextStyle">@style/textStyle</item>
        </style>
    
        <style name="LabelStyle">
            <item name="android:textColor">#00f</item>
        </style>
    
        <style name="textStyle">
            <item name="android:textColor">#009</item>
        </style>
    </resources>
    
  • MyView.java - 如果应用程序没有任何属性,则更改构造函数并设置默认 MyViewStyle。

    public MyView(Context context) {
        this(context, null, R.attr.myViewStyle,  R.style.MyViewStyle);
    }
    
    public MyView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, R.attr.myViewStyle,  R.style.MyViewStyle);
    }
    
    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, R.style.MyViewStyle);
    }
    
    public MyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(createThemeWrapper(context, defStyleAttr,defStyleRes), attrs, defStyleAttr, defStyleRes);
        initLayout();
    }
    
    private static Context createThemeWrapper(Context context, int styleAttr, int defaultStyle) {
        final TypedArray ta = context.obtainStyledAttributes(new int[]{styleAttr});
        int style1 = ta.getResourceId(0, defaultStyle);
        ta.recycle();
        return new ContextThemeWrapper(context, style1);
    }
    

因此,如果它没有在主要 Activity 样式中被覆盖或被覆盖的 labelStyle,它将采用默认的 labelStyle

关于android - 库的样式属性没有值,即使它们已明确设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47492342/

有关android - 库的样式属性没有值,即使它们已明确设置的更多相关文章

  1. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  2. ruby-openid:执行发现时未设置@socket - 2

    我在使用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

  3. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  4. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  5. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的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

  6. ruby - 如何使用文字标量样式在 YAML 中转储字符串? - 2

    我有一大串格式化数据(例如JSON),我想使用Psychinruby​​同时保留格式转储到YAML。基本上,我希望JSON使用literalstyle出现在YAML中:---json:|{"page":1,"results":["item","another"],"total_pages":0}但是,当我使用YAML.dump时,它不使用文字样式。我得到这样的东西:---json:!"{\n\"page\":1,\n\"results\":[\n\"item\",\"another\"\n],\n\"total_pages\":0\n}\n"我如何告诉Psych以想要的样式转储标量?解

  7. ruby-on-rails - 在混合/模块中覆盖模型的属性访问器 - 2

    我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah

  8. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  9. ruby-on-rails - 如何使用 instance_variable_set 正确设置实例变量? - 2

    我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击

  10. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

随机推荐