8

我有一个最小 sdk 16 到 23 的应用程序。我想尽可能地使用 Material Design。它还必须是全屏应用程序。包括 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>

在值/属性中:

<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的定义如此处所述。我不明白的行为是,当登录活动被打开时,一切正常,没有例外。但是,当我从带有片段的登录活动中打开另一个活动并且片段有一个具有这种样式的按钮时,我得到一个异常,说“错误膨胀类按钮”。当我从 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 
4

5 回答 5

11

将图像文件从“drawable-v24”文件夹复制到“drawable”文件夹。问题解决了。信用:https ://github.com/chrisjenx/Calligraphy/issues/417

于 2019-02-09T08:12:07.003 回答
1

我通过为 api 小于 21 创建自己的 appcompat 样式来修复它,

样式.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"/>

于 2016-03-03T09:29:24.543 回答
1

解决了。问题是充气机的属性是未知的(或未指定)。解决方案是在膨胀使用该属性的视图之前定义属性值。

解决方案1:具有该属性的样式不仅要在activity中指定,而且在具有该属性的某些视图的fragment中也必须指定。因此,将此行添加到片段的根视图膨胀行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>
于 2016-01-13T00:03:21.427 回答
0

我在使用 Kotlin 开发时遇到了类似的情况:为了解决这个问题,将应用程序的 drawable-24 中的内容复制到 drawable 中。这样做是为了支持较旧的 Android 设备。

于 2020-07-14T23:20:14.387 回答
0

正如我们在评论部分所讨论的,问题在于充气机不知何故无法识别?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 可以放置在相应的资源文件夹中,以适应特定的设备配置(屏幕尺寸、密度、语言等)。

于 2015-12-17T10:49:55.787 回答