简体   繁体   English

Android布局主题样式

[英]Android Layout Theme Styling

My question is as follows: I have some layouts that are by default light themed meaning that they are with a white background. 我的问题如下:我有一些默认情况下以浅色为主题的布局,即它们具有白色背景。 My Ripple effects look fine and my Widgets look fine. 我的涟漪效果看起来不错,我的小部件看起来也不错。

轻主题

The issue arises when I use my dark theme or black theme. 当我使用深色主题或黑色主题时,就会出现问题。 My Widgets become less visible as does my Ripple which is supposed to be white for darker themes. 我的窗口小部件变得不那么可见了,而对于较暗的主题来说,我的波纹应该是白色的。 The Ripple looks like this using the dark theme. 使用深色主题的涟漪看起来像这样。

黑暗主题

On the dark theme, the Ripple is only partly visible since the color of the background is #303030, lighter than the ripple which is #000000. 在深色主题上,由于背景色为#303030,比波纹#000000更浅,因此波纹仅部分可见。 Ideally, the Ripple should be white. 理想情况下,波纹应为白色。 On the black themed Layout, the ripple is invisible since both the background and the ripple are black. 在黑色主题版面中,由于背景和波纹均为黑色,因此波纹是不可见的。 I have actually fixed this issue by using 我实际上已经通过使用解决了这个问题

android:theme="ThemeOverlay.AppCompat.Dark" 

in my Layout. 在我的布局中。 If I use this the ripple is white and the Widgets are also more visible. 如果使用此选项,涟漪是白色的,并且小部件也更明显。 The issue is that when I click on a widget like the first switch, the app crashes and leaves me with a nonsensical error. 问题是,当我单击第一个开关之类的小部件时,应用程序崩溃并给我留下了无意义的错误。

EDIT: The error is as follows: 编辑:错误如下:

12-29 23:57:39.801 13055-13055/com.company.app E/AndroidRuntime: FATAL EXCEPTION: main
                                                           Process: com.company.app, PID: 13055
                                                           java.lang.IllegalArgumentException: Expected receiver of type com.company.app.SettingsActivity, but got android.view.ContextThemeWrapper
                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                               at android.view.View$DeclaredOnClickListener.onClick(View.java:4453)
                                                               at android.view.View.performClick(View.java:5204)
                                                               at android.widget.CompoundButton.performClick(CompoundButton.java:122)
                                                               at android.view.View$PerformClick.run(View.java:21153)
                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                               at android.os.Looper.loop(Looper.java:148)
                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

How can I theme my Widgets and my Ripple in such a way to display them properly (as described above) on darker themed layouts without error? 如何为小部件和Ripple设置主题,以便在较暗的主题布局上正确显示它们(如上所述)(没有错误)?

Thanks! 谢谢!

I had this problem with my AppCompat subclassed activity. 我的AppCompat子类别活动存在此问题。 I searched diligently and found the following issue posted in Android's issue tracker: https://code.google.com/p/android/issues/detail?id=174871 我经过认真搜索,发现在Android的问题跟踪器中发布了以下问题: https : //code.google.com/p/android/issues/detail? id =174871

There seems to be some issues recently with AppCompat's inflater, it appears that OnClick handlers generated from OnClick attributes in layout xml are not handling ContextWrappers properly. AppCompat的充气机最近似乎出现了一些问题,看来从布局xml中的OnClick属性生成的OnClick处理程序无法正确处理ContextWrappers。 The work-around is to remove the OnClick attributes from your xml and register OnClick listeners programmatically in your code. 解决方法是从XML中删除OnClick属性,并以编程方式在代码中注册OnClick侦听器。 I went around diligently registering all my click events explicitly in the code and removing all the onClick attributes from the widgets in my layout's xml file. 我努力地在代码中显式注册了所有单击事件,并从布局的xml文件中的小部件中删除了所有onClick属性。 That did fix it. 那确实解决了。

Today I discovered for my own application which uses AppCompat DayNight themes that a much easier fix was to remove "android:theme="@style/Theme.AppCompat.DayNight" from the root layout. I can now go back to using onClick attributes in the widget's xml. I don't know whether other AppCompat themes cause the same problem, but would not be surprised. My main activity sets the app's theme using AppCompatDelegate.setDefaultNightMode. Hope this helps, I was beside myself when I ran into this, my code worked great until I tested in Marshmallow, and there's scant help to be found as you can see with your question going unanswered for 6 months. 今天,我发现自己的应用程序使用了AppCompat DayNight主题,一个更简单的解决方法是从根目录布局中删除“ android:theme =“ @ style / Theme.AppCompat.DayNight”。现在,我可以回到在小部件的xml。我不知道其他AppCompat主题是否会导致相同的问题,但也不会感到惊讶。我的主要活动是使用AppCompatDelegate.setDefaultNightMode设置应用的主题。希望这对我有帮助,当我遇到这个问题时,在我使用棉花糖进行测试之前,我的代码一直都很好用,在6个月未解决问题的过程中,您几乎找不到任何帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM