目前我正在开发一个应用程序,但是我不知道有任何方法来检查系统是否已经处于黑暗模式。该应用程序是在Mac上使用JavaFX制作的。目前我有一个 CheckMenuItem,当检查时,使用 IF 语句加载“darkmode.css”文件。如果未选中 checkmenuitem 框,则删除该文件。
然而,我希望能够在操作系统处于黑暗模式并且不需要再次删除它时激活 css 文件,而不是检查 MenuItem。我感觉Java可能做不到这一点。如果有任何方法可以做到这一点,我将热衷于学习并对您如何实现它感兴趣。
简而言之,原生黑暗模式差不多。
我有这样的活动:
package com.nkdroid.daynighttheme;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.widget.TextView;
public class ModeActivity extends AppCompatActivity {
private TextView txtModeType;
int modeType;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_mode);
txtModeType = (TextView) findViewById(R.id.txtModeType);
modeType = AppCompatDelegate.getDefaultNightMode();
if (modeType == AppCompatDelegate.MODE_NIGHT_AUTO) {
txtModeType.setText("Default Mode: Auto");
} else if (modeType == AppCompatDelegate.MODE_NIGHT_YES) {
txtModeType.setText("Default Mode: Night");
} else if (modeType == AppCompatDelegate.MODE_NIGHT_NO) {
txtModeType.setText("Default Mode: Day");
}
}
}`
Run Code Online (Sandbox Code Playgroud)
如果默认模式设置为AUTO,是否可以获得当前激活的模式(白天或夜晚)?
最近,启用Android 9.0 Pie和Android OS的夜间模式后,我的用户向我发送了以下屏幕截图。
如您所见,股票名称不可见,因为股票名称使用了黑色。在正常的白色主题中,它看起来如下
这是我的代码用来突出显示文本颜色。在我的代码中,我始终假定下拉通知的背景是白色。(如果启用了暗模式,那是不正确的)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="NotificationTitle" parent="android:TextAppearance.Material.Notification.Title"></style>
</resources>
public SpannableString getFallBelowSpannableString(Context context) {
if (fallBelowSpannableString != null) {
return fallBelowSpannableString;
}
if (fallBelow == null) {
return null;
}
// The attributes you want retrieved
int[] attrs = {android.R.attr.textColor};
TypedArray ta = context.obtainStyledAttributes(R.style.NotificationTitle, attrs);
int textColor;
try {
textColor = ta.getColor(0, context.getResources().getColor(R.color.notification_symbol_name));
} finally {
ta.recycle();
}
final String notificationMessage = context.getString(R.string.falls_below_template, symbol, org.yccheok.jstock.watchlist.Utils.toStockPrice(fallBelow));
fallBelowSpannableString = new SpannableString(notificationMessage);
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(textColor);
fallBelowSpannableString.setSpan(foregroundColorSpan, …Run Code Online (Sandbox Code Playgroud)