我的应用程序中有多个主题,并且我已经为我的按钮定义了一个形状,我喜欢通过更改应用中的主题来更改颜色。 我已经完成了如下操作,但问题是固体标签的颜色,我不能在这里使用colorButtonNormal属性,
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners android:radius="7dp"/>
<solid android:color="?attr/colorButtonNormal" />
</shape>
在我的布局中,我定义了按钮标记:
<Button
android:id="@+id/sendCode"
android:layout_marginTop="30dp"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/sendCode"
android:textSize="@dimen/TextTwoSize"
android:textStyle="bold"
android:textColor="?android:textColorPrimary"
android:background="@drawable/bot_shape"/>
这是我的style.xml文件的一部分,用于定义主题:
<style name="myGreen" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimaryGreen</item>
<item name="colorAccent">@color/colorAccentGreen</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:colorBackground">@color/windowBackground</item>
<item name="colorButtonNormal">@color/colorAccentGreen</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkGreen</item>
<item name="android:textColorPrimary">@color/textColorPrimary</item>
<item name="android:textColorPrimaryInverse">@color/textColorPrimaryInverse</item>
<item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>
<item name="android:textColorSecondary">@android:color/white</item>
<item name="android:windowBackground">@color/windowBackground</item>
<item name="android:homeAsUpIndicator">@drawable/ic_keyboard_backspace_white_24dp</item>
<item name="android:itemBackground">@android:color/white</item>
<item name="android:textColor">@color/TextOneColor</item>
</style>
<style name="myRed" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimaryRed</item>
<item name="colorAccent">@color/colorAccentRed</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkRed</item>
<item name="android:textColorPrimary">@color/textColorPrimary</item>
<item name="android:textColorPrimaryInverse">@color/textColorPrimaryInverse</item>
<item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>
<item name="android:textColorSecondary">@android:color/white</item>
<item name="android:windowBackground">@color/windowBackground</item>
<item name="android:colorBackground">@color/windowBackground</item>
<item name="android:itemBackground">@android:color/white</item>
<item name="android:textColor">@color/TextOneColor</item>
<item name="colorButtonNormal">?attr/colorPrimary</item>
</style>
我收到错误:
Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
at android.content.res.TypedArray.getColor(TypedArray.java:326)
at android.graphics.drawable.GradientDrawable.inflate(GradientDrawable.java:951)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:881)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
at android.content.res.Resources.loadDrawable(Resources.java:1948)
at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
at android.view.View.<init>(View.java:3359)
at android.widget.TextView.<init>(TextView.java:477)
at android.widget.Button.<init>(Button.java:107)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:70)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:66)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:109)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1016)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1073)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:675)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at io.appogram.navigation.ConnectEmailActivity.onCreate(ConnectEmailActivity.java:30)
at android.app.Activity.performCreate(Activity.java:5066)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
at android.app.ActivityThread.access$600(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5536)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
<强> ?attr/ will only work for api>=21 in drawables
强>
对于以下版本,您不能在drawable中使用“?attr /”。
为每个主题尝试 adding separate drawable
并以编程方式进行设置。
您可以在布局xml文件中使用?attr /。所以你可以在按钮
中使用下面一行android:background="?attr/colorButtonNormal"
如果你想要塑造,每个主题使用单独的drawable是更好的。