android - Android 应用程序在尝试使用 ?attr/variable 时崩溃
问题描述
我正在尝试使用 ?attr/var 来设置颜色,但它一直在崩溃,说列表视图 xml 有错误。如果我使用正常的颜色属性,它可以正常工作。
2021-02-04 18:10:45.621 13812-13812/com.daford.sermonviewer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.viewer, PID: 13812
android.view.InflateException: Binary XML file line #3 in com.viewer:layout/list_item: Binary XML file line #3 in com.viewer:layout/list_item: Error inflating class android.widget.TextView
Caused by: android.view.InflateException: Binary XML file line #3 in com.viewer:layout/list_item: Error inflating class android.widget.TextView
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="textColor" format="color"/>
</resources>
样式.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="LightTheme" parent="AppTheme">
<item name="textColor">#000000</item>
</style>
<style name="DarkTheme" parent="AppTheme">
<item name="textColor">#ffffff</item>
</style>
</resources>
列表项.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="?attr/textColor"/>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.DarkTheme);
setContentView(R.layout.activity_main);
...
解决方案
使用 attr/textColor 内部样式
<style name="LightTheme" parent="AppTheme">
<item name=”android:textColor”>?attr/textColor</item>
</style>
然后将此样式应用于 textview。
有关详细信息,请参阅材料设计
推荐阅读
- r - 如何使用 ggplot2 coord_polar() 制作带圆圈的气泡图?
- javascript - Leaflet .on() 带有标记的事件监听器
- excel - 使用负计时器值运行宏
- flutter - 语音转文本仅识别数字
- python - 具有 3 阶张量的 TensorFlow 输入管道
- python-3.x - 使用雪花连接器时具有毫秒精度的日期对象
- java - SeekBar 可以工作,但不能自动运行搜索栏进度
- ios - 在横向启动我的 Swift 应用程序时设备屏幕宽度错误
- azure - Azure 中的设备场
- continuous-integration - Gitlab CD/CI:用户提供的路径 build/ 不存在