我想为我的应用程序提供2个可选主题.为了做到这一点,我定义了一些属性,如下所示:
<attr format="color" name="item_background" />
Run Code Online (Sandbox Code Playgroud)
然后,我创建了两个主题,如下所示:
<style name="ThemeA">
<item name="item_background">#123456</item>
</style>
<style name="ThemeB">
<item name="item_background">#ABCDEF</item>
</style>
Run Code Online (Sandbox Code Playgroud)
这种方法效果很好,允许我轻松地创建和修改几个主题.问题是它似乎只能在Views中使用,而不能在Drawables中使用.
例如,引用布局内View的值可以:
<TextView android:background="?item_background" />
Run Code Online (Sandbox Code Playgroud)
但是在Drawable中做同样的事情并不是:
<shape android:shape="rectangle">
<solid android:color="?item_background" />
</shape>
Run Code Online (Sandbox Code Playgroud)
运行应用程序时出现此错误:
java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
Run Code Online (Sandbox Code Playgroud)
如果不是?item_background我使用硬编码颜色,它可以工作,但这不允许我使用我的主题.我也试过?attr:item_background,但同样的事情发生了.
我怎么能这样做?为什么它在Views中有效但在Drawables中无效?我在文档中的任何地方都找不到这个限制......
我在应用程序的登录页面中使用TextInputLayout UI机制,一切都很好,除了在这个设备上运行4.4.2 android版本的设备我有一个例外.
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include android:id="@+id/loginInfiLogoRL"
layout="@layout/login_infi_logo_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="-3dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"/>
<!--EMAIL-->
<android.support.design.widget.TextInputLayout
android:id="@+id/email_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/loginInfiLogoRL"
android:theme="@style/TextLabel">
<EditText
android:id="@+id/etUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/login_screen_email_hint"
android:imeOptions="actionNext"
android:inputType="textEmailAddress"
android:maxLines="1"
android:nextFocusDown="@+id/etPassword"
android:singleLine="true"
android:textColor="@android:color/white"
android:textColorHighlight="@color/Orange"
android:textColorHint="@android:color/white" />
</android.support.design.widget.TextInputLayout>
<RelativeLayout
android:id="@+id/rlPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_below="@id/email_wrapper">
<!--PASSWORD-->
<android.support.design.widget.TextInputLayout
android:id="@+id/password_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/TextLabel">
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/login_screen_password_hint"
android:imeOptions="actionNext"
android:inputType="textPassword"
android:maxLines="1"
android:nextFocusDown="@+id/bSignIn"
android:shadowColor="@color/Orange"
android:singleLine="true"
android:textColor="@android:color/white"
android:textColorHint="@android:color/white" />
</android.support.design.widget.TextInputLayout>
<!--FORGOT-->
<TextView
android:id="@+id/tvForgotPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:paddingBottom="14dp"
android:paddingRight="10dp"
android:text="@string/login_screen_forgot_password_text"
android:textColor="@color/Orange" …Run Code Online (Sandbox Code Playgroud) android colors android-edittext inflate-exception android-textinputlayout