我正在尝试使用选择器中stlyle中定义的颜色,但它导致了一个Resources $ NotFoundException.
首先,我向attr.xml添加了一个新属性:
<resources>
<attr name="unread_background" format="color" />
</resources>
Run Code Online (Sandbox Code Playgroud)
然后我在styles.xml中定义了attr值:
<style name="ThemeNoTitleBar" parent="android:Theme.NoTitleBar">
<item name="unread_background">#000000</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后我尝试在我的选择器定义中使用该attr:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- other states snipped -->
<item android:state_selected="false"
android:drawable="?unread_background" />
</selector>
Run Code Online (Sandbox Code Playgroud)
最后,活动在清单中使用ThemeNoTitleBar样式主题.
我也尝试在colors.xml中创建一个颜色并让它使用新的attr,但也失败了.
我显然错过了一些东西,但我不知道该怎么做才能修复它.我的目的是创建多个主题,并让选择器使用当前所选主题中的颜色.
阅读参考主题属性后,我试图在我设置的自定义主题中引用属性的值.
我将用户定义的样式应用于 CheckedTextView
<CheckedTextView
android:id="@+id/contactInfo"
style="@style/ListViewCheckedTextViewRowStyle" >
</CheckedTextView>
Run Code Online (Sandbox Code Playgroud)
用户定义的样式定义为:
<style name="ListViewCheckedTextViewRowStyle" parent="@style/ListViewRowStyle">
<item name="android:checkMark">?android:listChoiceIndicatorMultiple</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我创建的主题定义为:
<style name="Theme.Yellowgreen" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:listChoiceIndicatorMultiple">@drawable/btn_check_holo_light</item>
</style>
Run Code Online (Sandbox Code Playgroud)
但是,显示的checkMark样式是设备的默认主题的drawable,而不是我的用户定义的drawable.
我可以显示可绘制的唯一方法是:
<style name="ListViewCheckedTextViewRowStyle" parent="@style/ListViewRowStyle">
<item name="android:checkMark">@drawable/btn_check_holo_light</item>
</style>
Run Code Online (Sandbox Code Playgroud)
但这违背了覆盖此属性的整个目的,特别是因为我想在多个主题中覆盖此属性.
我在onCreate()我的方法中设置主题Activity:
public void onCreate(Bundle savedInstanceState) {
this.setTheme(R.style.Theme_Yellowgreen);
super.onCreate(savedInstanceState);
// ...
}
Run Code Online (Sandbox Code Playgroud)
我还尝试在AndroidManifest.xml文件中设置主题,如:
<application android:theme="@style/Theme.Yellowgreen" >
Run Code Online (Sandbox Code Playgroud)
但那没用.怎么可能出错?
我刚刚创建了一个小型示例项目,看起来我上面发布的代码正在运行.所以我必须有一些其他样式覆盖这个属性,或者它可能与我的布局xml文件有关.
在我的大项目中,我有两个Fragments内Activity.两者Fragments都有Listviews支持Adapters.在Fragment A所述getView()的方法Adapter如下:
public View getView(final int position, View convertView, …Run Code Online (Sandbox Code Playgroud) 我尝试在文本颜色选择器元素内使用“?colorAccent”,但每当激活相关状态时,文本就会显示红色而不是我的实际 colorAccent 值。我已将问题隔离到最少的文件中,并在下面发布了相关片段。我还在这里上传了完整的项目: https: //github.com/danh32/ColorAccentSelector如果有任何进一步的帮助。
1)我将 ListView 设置为单选模式,以便可以检查其行。
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setAdapter(new ItemAdapter());
}
}
Run Code Online (Sandbox Code Playgroud)
2)每一行只是一个 CheckedTextView,因此我可以根据选中状态操纵其 textColor。行.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="16sp"
android:padding="8sp"
android:textColor="@color/row_text_color" />
Run Code Online (Sandbox Code Playgroud)
3) @color/row_text_color 具有以下内容,未选中时应显示纯黑色,选中时应显示我的 colorAccent 值:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="?colorAccent" />
<item android:color="@android:color/black" />
</selector>
Run Code Online (Sandbox Code Playgroud)
4)我的应用程序的主题是这样的,它应该将 ?colorAccent 值设置为##ff4081(材质粉红色 A200):
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- …Run Code Online (Sandbox Code Playgroud) 我使用自定义属性遇到了问题.请帮忙..
我在attr.xml中定义了自定义属性
<attr format="reference" name="btnPressed" />
<attr format="reference" name="btnNormal" />
Run Code Online (Sandbox Code Playgroud)
我有一个主题
<style name="MyTheme" parent="@style/Theme.Sherlock.Light.NoActionBar">
<item name="btnPressed">@drawable/fav_icon</item>
<item name="btnNormal">@drawable/not_fav_icon</item>
</style>
Run Code Online (Sandbox Code Playgroud)
在上面的fav_icon和not_fav_icon是我的图像.
我有一个为切换按钮定义的选择器favorite_btn.xml.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="?attr/btnPressed"/>
<item android:state_checked="false" android:drawable="?attr/btnNormal"/>
<item android:drawable="?attr/btnNormal"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
现在我将选择器设置为我的切换按钮.
<ToggleButton
android:id="@+id/station_fav_star"
android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/favorite_btn"
android:textOff=""
android:textOn="" />
Run Code Online (Sandbox Code Playgroud)
我正在将这个主题应用到我的活动中.但我正在获得Xml Parse Exception,如下所述.
引起:org.xmlpull.v1.XmlPullParserException:二进制XML文件行#11:标记需要一个'drawable'属性或定义drawable的子标记
我正在使用相同的方法使用自定义属性将图像设置为普通按钮,它正在工作.但是当在选择器中定义自定义属性时,它不起作用.
如果我遗失任何东西,请告诉我.
我已经检查过此链接 Android颜色选择器不适用于自定义属性
因为它表示颜色变化.就我而言,它们是可绘制的.如果还有其他方法,请提出解决方案.
提前致谢..
我试图让用户设置自己的颜色主题.我已经设法完成了这个
attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="extra_light" format="reference" />
</resources>
Run Code Online (Sandbox Code Playgroud)
styles.xml
<style name="Green" parent="@style/AppTheme">
<item name="extra_light">@color/extra_light_green</item>
</style>
Run Code Online (Sandbox Code Playgroud)
colors.xml
<resources>
<color name="extra_light_green">#C5E26D</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
这适用于大多数应用程序,但我有一个以前的选择器
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/extra_light_green" />
</selector>
Run Code Online (Sandbox Code Playgroud)
至
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="?attr/extra_light" />
</selector>
Run Code Online (Sandbox Code Playgroud)
现在它崩溃了.
这是logcat,关于如何解决这个问题的任何想法?
FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:683)
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at com.android.internal.policy.impl.MiuiPhoneLayoutInflater.onCreateView(MiuiPhoneLayoutInflater.java:44)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:730)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:816)
at android.view.LayoutInflater.inflate(LayoutInflater.java:559)
at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
at android.view.LayoutInflater.inflate(LayoutInflater.java:419)
Run Code Online (Sandbox Code Playgroud)
编辑
这是我应用选择器的地方
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp" …Run Code Online (Sandbox Code Playgroud)