我有一个min sdk 16到23的应用程序。我想尽可能多地使用Material设计。它也必须是全屏应用程序。包括AppCompat支持库。现在我使用一些按钮进行登录活动:
<Button
android:id="@+id/act_logon_btn_logon"
style="@style/ButtonDefault"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/act_logon_logon" />
样式如下(values / styles.xml):
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
<style name="AppThemeBase" parent="Theme.AppCompat.Light.NoActionBar.FullScreen"></style>
<style name="AppTheme" parent="AppThemeBase"></style>
<style name="ButtonDefault" parent="Widget.AppCompat.Button">
<item name="android:textSize">?attr/font_medium</item>
</style>
<style name="FontStyle"></style>
<style name="FontStyle.Small">
<item name="font_small">12sp</item>
<item name="font_medium">14sp</item>
<item name="font_large">16sp</item>
</style>
<style name="FontStyle.Medium">
<item name="font_small">16sp</item>
<item name="font_medium">18sp</item>
<item name="font_large">20sp</item>
</style>
<style name="FontStyle.Large">
<item name="font_small">20sp</item>
<item name="font_medium">22sp</item>
<item name="font_large">24sp</item>
</style>
在值/ attrs中:
<declare-styleable name="FontStyle">
<attr name="font_small" format="dimension" />
<attr name="font_medium" format="dimension" />
<attr name="font_large" format="dimension" />
</declare-styleable>
属性 font_medium 的定义如here所述。我不明白的行为是,当登录活动打开时,一切正常,没有例外。但是当我从Logon活动中打开另一个活动时,片段里面有片段并且片段有一个带有这种风格的按钮,我得到一个异常说&#34;错误膨胀类Button&#34;。当我从ButtonDefault样式中删除 textSize 属性时,一切正常。为什么它在某个地方工作而在某个地方不工作?自定义属性有问题吗?
我尝试使用该样式创建values-v21 / styles.xml但没有效果。
您还可以解释以下父母风格定义之间的差异吗?我迷路了。
parent="Widget.AppCompat.Button"
parent="@style/Widget.AppCompat.Button"
parent="android:Widget.Button"
parent="android:Widget.AppCompat.Button"
parent="android:Widget.Material.Button"
parent="android:style/Widget.Material.Button"
谢谢。
编辑1 - 例外:
12-17 10:56:53.950: E/AndroidRuntime(7251): FATAL EXCEPTION: main
12-17 10:56:53.950: E/AndroidRuntime(7251): Process: test.android, PID: 7251
12-17 10:56:53.950: E/AndroidRuntime(7251): java.lang.RuntimeException: Unable to start activity ComponentInfo{test.dci.android/test.dci.android.view.activity.TransactionActivity}: android.view.InflateException: Binary XML file line #49: Error inflating class Button
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.app.ActivityThread.access$900(ActivityThread.java:177)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.os.Handler.dispatchMessage(Handler.java:102)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.os.Looper.loop(Looper.java:145)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.app.ActivityThread.main(ActivityThread.java:5951)
12-17 10:56:53.950: E/AndroidRuntime(7251): at java.lang.reflect.Method.invoke(Native Method)
12-17 10:56:53.950: E/AndroidRuntime(7251): at java.lang.reflect.Method.invoke(Method.java:372)
12-17 10:56:53.950: E/AndroidRuntime(7251): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
12-17 10:56:53.950: E/AndroidRuntime(7251): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
12-17 10:56:53.950: E/AndroidRuntime(7251): Caused by: android.view.InflateException: Binary XML file line #49: Error inflating class Button
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
12-17 10:56:53.950: E/AndroidRuntime(7251): at test.dci.android.view.fragment.TransactionFragment.onCreateView(TransactionFragment.java:71)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1226)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1328)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2284)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:314)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
12-17 10:56:53.950: E/AndroidRuntime(7251): at test.dci.android.view.activity.TransactionActivity.onCreateView(TransactionActivity.java:1)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:740)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
12-17 10:56:53.950: E/AndroidRuntime(7251): at test.dci.android.view.activity.TransactionActivity.onCreate(TransactionActivity.java:31)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.app.Activity.performCreate(Activity.java:6289)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
12-17 10:56:53.950: E/AndroidRuntime(7251): ... 10 more
12-17 10:56:53.950: E/AndroidRuntime(7251): Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 12
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:582)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.widget.TextView.<init>(TextView.java:1400)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.widget.Button.<init>(Button.java:115)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.widget.Button.<init>(Button.java:108)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:62)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:58)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:98)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:938)
12-17 10:56:53.950: E/AndroidRuntime(7251): at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:992)
12-17 10:56:53.950: E/AndroidRuntime(7251): at
答案 0 :(得分:1)
解决。问题是该属性是inflater的未知(或未指定)。解决方案是在膨胀使用该属性的视图之前定义属性值。
解决方案1:具有此属性的样式不仅必须在活动中指定,还必须在具有此属性的某些视图的片段中指定。因此,在扩展片段根视图的行之前,将此行添加到片段的onCreateView()
:
getContext().getTheme().applyStyle(fontStyleResId, true);
解决方案2 :直接在应用主题中设置属性值:
<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>
答案 1 :(得分:1)
我通过为api少于21,
创建自己的appcompat样式来修复它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>
样式-v21.xml
<style name="ColoredButtonAppcompat" parent="Widget.AppCompat.Button.Colored"/>
答案 2 :(得分:0)
正如我们在评论部分讨论的那样,问题是inflater以某种方式无法识别?attr/font_medium
,从而引发异常。
您可以使用dimens.xml文件指定自定义维度,如下所示:
<resources>
<dimen name="small">14sp</dimen>
<dimen name="medium">16sp</dimen>
<dimen name="large">18sp</dimen>
</resources>
然后在styles.xml中引用这些值:
<style name="ButtonDefault" parent="Widget.AppCompat.Button">
<item name="android:textSize">@dimen/medium</item>
</style>
与其他资源一样,dimens.xml可以放在相应的资源文件夹中,以适应特定的设备配置(屏幕尺寸,密度,语言等)。
答案 3 :(得分:0)
将图像文件也从“ drawable-v24”文件夹复制到“ drawable”文件夹。问题解决了。功劳:https://github.com/chrisjenx/Calligraphy/issues/417
答案 4 :(得分:0)
在使用Kotlin进行开发时,我遇到了类似的情况: 为了解决此问题,请将应用程序中drawable-24的内容复制到drawable。 这样做是为了支持较旧的Android设备。