使用:
com.google.android.material:material:1.4.0如果我在带有TextView的片段/活动中有一个布局xml文件:
<TextView
style="@style/TextAppearance.MaterialComponents.Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />如果我在明/暗模式之间切换,文本颜色会相应地切换。
但是,如果我创建了一个自定义视图:
class MyView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0,
defStyleRes: Int = 0
) : LinearLayout(context, attrs, defStyle, defStyleRes) { ... }并且该视图包含相同的文本视图,则文本颜色在夜间模式下不会改变。
我从一个片段初始化视图:
new MyView(this.getActivity().getBaseContext());我还尝试直接应用基本主题:
new MyView(this.getActivity().getBaseContext(), null, R.id.AppTheme);此外,出于一些奇怪的原因,我可以通过在适当的亮/暗文件夹中创建自己的文本颜色来解决这个问题,这样可以获得亮/暗之间的变化:
<TextView
style="@style/TextAppearance.MaterialComponents.Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/my_text_color" />发布于 2021-11-19 20:30:06
事实证明我传递了错误的上下文。
创建视图时:
new MyView(this.getActivity().getBaseContext());应该是:
new MyView(this.getActivity());https://stackoverflow.com/questions/70039903
复制相似问题