android – Library的样式属性没有值,即使它们是显式设置的

目前的答案没有帮助,这个问题没有答案

我创建了一个带有自定义视图的库,可以在创建时扩展布局.布局中的视图使用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>

自定义视图代码:

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

public MyView(Context context,@Nullable AttributeSet attrs) {
    this(context,attrs,@Nullable AttributeSet attrs,int defStyleAttr) {
    this(context,defStyleAttr,AttributeSet attrs,int defStyleAttr,int defStyleRes) {
    super(createThemeWrapper(context,R.style.MyViewStyleLight),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)
      [...]

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

我应该如何设计我的图书馆风格?我几乎可以肯定我做的每一件事都和SublimePicker library一样,但它不会起作用.使用ContextThemeWrapper的部分有一点不同,但它可能不是问题.我觉得我忘记了一个小东西,使得属性没有价值,有些东西没有连接,我不知道.

我知道这是一个很长的问题,但它不能更简洁,我尽可能地简化了所有内容.我更改了上一版本问题中的大部分信息,使其完全不同.这两个答案现在根本不相关,而不是它们曾经是.赏金自动得到奖励.

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

解决方法

这个答案是基于我从你和Vinayak B之间的问题和对话中理解的.如果我曲解,请纠正我.

在app app和lib中,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>

> lib styles.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,R.style.MyViewStyle);
}

public MyView(Context context,defStyleRes),int defaultStyle) {
    final TypedArray ta = context.obtainStyledAttributes(new int[]{styleAttr});
    int style1 = ta.getResourceId(0,style1);
}

因此,如果未在主活动样式中覆盖或重写labelStyle,它将采用默认labelStyle

相关文章

Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...