【问题标题】:Changing minSdkVersion from 15 to 16将 minSdkVersion 从 15 更改为 16
【发布时间】:2017-01-10 15:29:37
【问题描述】:

当我更改我的 minSdkVersion 时,我收到了这个错误:

android.view.InflateException: Binary XML file line #43: Error inflating class TextView

在我进行更改之前它运行良好。

这是我的风格:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

    <style name="AppTheme.Dark" parent="Theme.AppCompat.NoActionBar">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorAccent">@color/white</item>
        <item name="colorControlNormal">@color/greyDark</item>
        <item name="colorControlActivated">@color/off_white</item>
        <item name="android:windowBackground">@color/pink</item>
        <item name="android:fontFamily">sans-serif-light</item>
        <item name="android:textStyle">normal</item>
        <item name="android:windowContentTransitions" tools:targetApi="lollipop">true</item>
        <item name="android:windowAllowEnterTransitionOverlap" tools:targetApi="lollipop">true</item>
        <item name="android:windowAllowReturnTransitionOverlap" tools:targetApi="lollipop">true</item>
        <item name="android:windowSharedElementEnterTransition" tools:targetApi="lollipop">@android:transition/move</item>
        <item name="android:windowSharedElementExitTransition" tools:targetApi="lollipop">@android:transition/move</item>

    </style>
    <style name="AlertDialogCustom" parent="@android:style/Theme.Dialog">
        <item name="android:textColor">@color/pink</item>
        <item name="android:typeface">normal</item>
        <item name="android:height">5dp</item>
        <item name="android:textSize">20sp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:background">@color/white</item>
    </style>

我猜样式的父属性有问题。

【问题讨论】:

  • 再次更改 misSDKVersion 并检查它是否工作?
  • @ReadyAndroid 不工作我已经检查了几次 Pratik 的答案对我没有帮助,因为我已经有了 appcompact 版本 24.2.1
  • 我要求在任何 minSDKVersion 中检查它是否工作,如果它不工作,那么请添加您的 xml 文件以解决错误。
  • 等一下,我得到了这个外观,我有 appcompact 版本 'com.android.support:appcompat-v7:24.2.1' 但我使用的所有样式都很简单,v11,v14,v17这有什么关系,因为之前它没有显示任何错误
  • @ReadyAndroid 我说它适用于 minsdkVersion 15 我测试过阅读我的问题

标签: android


【解决方案1】:

如果您要更改版本,则还要更改支持库。您必须更改支持库,例如对于 api 23,您必须将 appcompact 库版本更改为 23。 希望对您有所帮助。:)

【讨论】:

  • 不,我的 appcompact 库版本是:compile 'com.android.support:appcompat-v7:24.2.1'
  • 更改应用主题
  • 好的,但是有什么变化?
  • 将应用主题更改为浅色
【解决方案2】:

你应该尝试改变

<style name="AlertDialogCustom" parent="@android:style/Theme.Dialog">

<style name="AlertDialogCustom" parent="Theme.AppCompat.Dialog">

【讨论】:

    【解决方案3】:

    这是您在样式中使用的字体系列的问题。可能是您的assests>>fonts 文件夹中没有无衬线字体。因此,请检查如何在 android 中使用自定义字体。

    以下是要遵循的步骤:

    Android 工作室:

    Step1:将字体系列文件添加到应用程序

    A) 转到(项目文件夹)

    B) 然后是 app>src>main

    C) 在主文件夹中创建文件夹“assets>fonts”。

    D) 将“abc.ttf”或“abc.otf”字体文件放入字体文件夹。

    第 2 步: 现在在 res 文件夹下创建 attrs.xml 如果它不存在并添加 declare-styleable

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
    <declare-styleable name="Font">
    <attr name="typeface">
    <enum name="FuturaLT" value="0" />
    <enum name="FuturaLT_Heavy" value="1" />
    <enum name="FuturaLT_Light" value="2" />
    </attr>
    </declare-styleable>
    
    </resources>
    

    //这里FuturaLT/FuturaLT_Heavy/FuturaLT_Light是文件名

    注意: – 枚举名称应该是带有下划线(_)的字体类型名称,这样您就可以很容易地理解在您的本机代码中字体系列的用法。没有特殊字符或其他字母,否则在gen文件夹中相同属性id会出错。

    第 3 步:创建自定义视图(Button、TextView、EditText)类:-

    package com.myapp.views;
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    import com.myapp.R;
    
    public class CustomTextView extends TextView  {
    
        public CustomTextView(Context context) {
            super(context);
        }
    
        public CustomTextView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            try {
                TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Font);
                int font = a.getInt(R.styleable.Font_typeface, 0);
                a.recycle();
                String str;
                switch (font) {
                    case 1:
                        str = "fonts/FuturaLT.otf";
                        break;
                    case 2:
                        str = "fonts/FuturaLT_Heavy.otf";
                        break;
                    case 3:
                        str = "fonts/FuturaLT_Light.otf";
                        break;
                    default:
                        str = "fonts/FuturaLT.otf";
                        break;
                }
    
                setTypeface(FontManager.getInstance(getContext()).loadFont(str));
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @SuppressWarnings("unused")
        private void internalInit(Context context, AttributeSet attrs) {
    
        }
    }
    

    第四步:添加 FontManager.java 支持类

    package com.myapp.views;
    
    import android.content.Context;
    import android.graphics.Typeface;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class FontManager {
        private Map<String, Typeface> fontCache = new HashMap<String, Typeface>();
        private static FontManager instance = null;
        private Context mContext;
    
        private FontManager(Context mContext2) {
            mContext = mContext2;
        }
    
        public synchronized static FontManager getInstance(Context mContext) {
            if (instance == null) {
                instance = new FontManager(mContext);
            }
            return instance;
        }
    
        public Typeface loadFont(String font) {
            if (false == fontCache.containsKey(font)) {
                fontCache.put(font, Typeface.createFromAsset(mContext.getAssets(), font));
            }
            return fontCache.get(font);
        }
    }
    

    第 5 步:在 XML 布局中的使用

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <com.myapp.views.CustomTextView
            android:id="@+id/tv_time_slot"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            app:typeface="FuturaLT" />
    </LinearLayout>
    

    也可以直接在java代码中使用:

    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/FuturaLT.ttf");
    tvText.setTypeface(tf);
    

    注意:-如果这里不使用FontManager应用字体直接使用 那么您将无法在图形预览中看到您的视图。

    【讨论】:

    • 请看讨论我在那里问过一些事情
    猜你喜欢
    • 2019-02-19
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多