Error inflating class Button in Android

前端 未结 5 1541
暗喜
暗喜 2021-01-05 16:19

I have an application with min sdk 16 up to 23. I want to use Material design as much as possible. It also has to be fullscreen app. AppCompat support library is included. N

相关标签:
5条回答
  • 2021-01-05 17:02

    Solved. The problem is that the attribute is uknown (or not specified) for the inflater. The solution is to define attribute values before inflating views that use the attribute.

    Solution 1: The style with this attribute must be specified not only in the activity, but also in the fragment that has some views with this attribute. So add this line to fragment's onCreateView() before the line that inflates fragment's root view:

    getContext().getTheme().applyStyle(fontStyleResId, true);
    

    Solution 2: Set attributes value directly in application's theme:

    <style name="AppThemeBase" parent="Theme.AppCompat.Light.NoActionBar.FullScreen">
        <item name="font_small">@dimen/font_size_medium_small</item>
        <item name="font_medium">@dimen/font_size_medium_medium</item>
        <item name="font_large">@dimen/font_size_medium_large</item>
    </style>
    
    0 讨论(0)
  • 2021-01-05 17:03

    I fixed it by creating own appcompat style for api less then 21,

    styles.xml

      <style name="ColoredButtonAppcompat">
        <item name="android:textAppearance">@style/ButtonTextStyle</item>
        <item name="android:background">@drawable/colored_button_background</item>
        <item name="android:minHeight">48dip</item>
        <item name="android:minWidth">88dip</item>
        <item name="android:focusable">true</item>
        <item name="android:clickable">true</item>
        <item name="android:gravity">center_vertical|center_horizontal</item>
    </style>
    
    <style name="ButtonTextStyle" >
        <item name="android:textSize">14dp</item>
        <item name="android:textColor">@android:color/white</item>
    </style>
    

    styles-v21.xml

    <style name="ColoredButtonAppcompat" parent="Widget.AppCompat.Button.Colored"/>

    0 讨论(0)
  • 2021-01-05 17:04

    COPY your image files from "drawable-v24" folder to the "drawable" folder as well. Problem solved. credit: https://github.com/chrisjenx/Calligraphy/issues/417

    0 讨论(0)
  • 2021-01-05 17:04

    I faced a similar situation while developing using Kotlin: In order to solve this issue, copy the app the content in drawable-24 to drawable. This is done to support older Android devices.

    0 讨论(0)
  • 2021-01-05 17:05

    As we discussed in the comment section, the problem is that the inflater somehow fails to recognize ?attr/font_medium, thus raising an exception.

    You can use the dimens.xml file to specify custom dimensions, like this:

    <resources> 
        <dimen name="small">14sp</dimen>
        <dimen name="medium">16sp</dimen>
        <dimen name="large">18sp</dimen>
    </resources>
    

    and then in your styles.xml you refer to these values like this:

    <style name="ButtonDefault" parent="Widget.AppCompat.Button">
        <item name="android:textSize">@dimen/medium</item>
    </style>
    

    As every other resource, dimens.xml can be placed in the according resource folders to accomodate to a specific device configuration (screen dimensions, density, language, etc.).

    0 讨论(0)
提交回复
热议问题