这里很奇怪的问题。我带着它兜圈子!
日/夜模式似乎在我的应用程序中运行良好......但INVISIBLE
仅适用于可绘制对象,它不适用于初始加载。
我有 2 个 xml 可绘制文件夹:
drawable
并drawable-night
具有匹配的 .png 文件名
我有两个值文件夹:
values
with colors.xml
, styles.xml
, ic_launcher_background.xml
and strings.xml
- and also -
values-night
with colors.xml
andstyles.xml
在应用程序中,我使用切换开关为日/夜模式设置模式,如下所示。这似乎工作正常。
dayNightMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
System.out.println("Not Checked");
InitApplication.getInstance().setIsNightModeEnabled(true, context);
Intent intent = getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(intent);
dayNightMode.setText("Night Mode");
} else {
System.out.println("Checked");
InitApplication.getInstance().setIsNightModeEnabled(false, context);
Intent intent = getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(intent);
dayNightMode.setText("Day Mode");
}
}
});
上面代码调用的InitApplication
类是:
package com.dmurphy.remotescrumpoker;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
public class InitApplication extends Application {
public static final String NIGHT_MODE = "NIGHT_MODE";
private boolean isNightModeEnabled = false;
private static InitApplication singleton = null;
public static InitApplication getInstance() {
if(singleton == null)
{
singleton = new InitApplication();
}
return singleton;
}
@Override
public void onCreate() {
super.onCreate();
singleton = this;
Context appContext = this.getApplicationContext();
SharedPreferences sharedPreferences = appContext.getSharedPreferences("com.dmurphy.remotescrumpoker", Context.MODE_PRIVATE);
isNightModeEnabled = sharedPreferences.getBoolean(NIGHT_MODE, true);
Log.i("onCreate1_", String.valueOf(isNightModeEnabled));
}
public boolean isNightModeEnabled(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences("com.dmurphy.remotescrumpoker", Context.MODE_PRIVATE);
isNightModeEnabled = sharedPreferences.getBoolean(NIGHT_MODE, true);
Log.i("onCreate11_", String.valueOf(isNightModeEnabled));
return isNightModeEnabled;
}
public void setIsNightModeEnabled(boolean isNightModeEnabled, Context context) {
this.isNightModeEnabled = isNightModeEnabled;
Log.i("onCreate111_", String.valueOf(isNightModeEnabled));
SharedPreferences sharedPreferences = context.getSharedPreferences("com.dmurphy.remotescrumpoker", Context.MODE_PRIVATE);
sharedPreferences.edit().putBoolean(NIGHT_MODE, isNightModeEnabled).apply();
Log.i("onCreate1111_", String.valueOf(isNightModeEnabled));
}
}
我也有Activity_Poker
活动。一切正常,如果我更改开关,应用程序就会更改。
我也有一个启动活动Activity_Splash
,并在onCreate
我检查模式:
if (InitApplication.getInstance().isNightModeEnabled(context)) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
Log.i(TAG, "onCreate1_" + "Mode Night Yes");
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
Log.i(TAG, "onCreate1_" + "Mode Night No");
}
setContentView(R.layout.activity__splash);
启动工作正常。当我关闭应用程序并重新启动时,两种模式的背景颜色等都是正确的。我VISIBLE
在活动中始终有 3 个可绘制对象。这些在两种替代模式之间也可以很好地改变。
但是,我有 9 个默认情况下不可见的可绘制对象。这些在创建时变为可见,但它们总是在每次加载时显示 DAY_MODE 。
注意: drawable不在错误的文件夹中。正确的颜色位于正确的文件夹中。9 个不工作的 .png 文件的颜色与VISIBLE
工作正常的 3 个可绘制对象相匹配。只是默认INVISIBLE
的不是从drawables-night
文件夹中填充的onCreate
。
如果我使用 inApp 用户菜单重新启动 Activity,则使用适用的NIGHT_MODE
或DAY_MODE
可绘制对象正确重新创建 Activity。但是 NIGHT_MODE 可绘制对象(默认情况下是不可见的)永远不会在启动时使用。
预期结果:在启动时,不可见的 ImageView 更改为可见并填充drawables
为 forDAY_MODE
或drawables-night
for NIGHT_MODE
。
实际结果:在启动时,不可见的 ImageView 更改为可见并填充drawables
为 forDAY_MODE
或drawables
for NIGHT_MODE
。
Activity_Poker.java
public class Activity_Poker extends AppCompatActivity {
[snip]
Activity passedActivity = Activity_Poker.this;
[snip]
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_poker);
drawerLayout = findViewById(R.id.drawer_layout);
[snip]
final String methodName = "onCreate";
mAuth = FirebaseAuth.getInstance();
String userId = mAuth.getUid();
[snip]
//Setup the UI for the selected game type
int cardType = sharedPreferences.getInt("cardType", 0);
Log.i(TAG, "Selected SharedPref Record: " + cardType);
callSetupUI.setupLocalActive(passedActivity, cardType);
[snip]
}
注意: cardType
上面正确显示了 Logcat 中的 == 5。
设置UI.java
public void setupLocalActive(Activity passedActivity, int cardType) {
ImageView card4 = passedActivity.findViewById(R.id.pokerCard4);
ImageView card4img = passedActivity.findViewById(R.id.pokerCard4img);
ImageView card5 = passedActivity.findViewById(R.id.pokerCard5);
ImageView card5img = passedActivity.findViewById(R.id.pokerCard5img);
ImageView card6 = passedActivity.findViewById(R.id.pokerCard6);
ImageView card6img = passedActivity.findViewById(R.id.pokerCard6img);
ImageView card7 = passedActivity.findViewById(R.id.pokerCard7);
ImageView card7img = passedActivity.findViewById(R.id.pokerCard7img);
ImageView card8 = passedActivity.findViewById(R.id.pokerCard8);
ImageView card8img = passedActivity.findViewById(R.id.pokerCard8img);
ImageView card9 = passedActivity.findViewById(R.id.pokerCard9);
ImageView card9img = passedActivity.findViewById(R.id.pokerCard9img);
ImageView card11 = passedActivity.findViewById(R.id.pokerCard11);
ImageView card11img = passedActivity.findViewById(R.id.pokerCard11img);
TextView card4TextView = passedActivity.findViewById(R.id.textViewPokerCard4);
TextView card5TextView = passedActivity.findViewById(R.id.textViewPokerCard5);
TextView card6TextView = passedActivity.findViewById(R.id.textViewPokerCard6);
TextView card7TextView = passedActivity.findViewById(R.id.textViewPokerCard7);
TextView card8TextView = passedActivity.findViewById(R.id.textViewPokerCard8);
TextView card9TextView = passedActivity.findViewById(R.id.textViewPokerCard9);
TextView card10TextView = passedActivity.findViewById(R.id.textViewPokerCard10);
TextView card11TextView = passedActivity.findViewById(R.id.textViewPokerCard11);
TextView card12TextView = passedActivity.findViewById(R.id.textViewPokerCard12);
TextView card13TextView = passedActivity.findViewById(R.id.textViewPokerCard13);
TextView card14TextView = passedActivity.findViewById(R.id.textViewPokerCard14);
TextView card15TextView = passedActivity.findViewById(R.id.textViewPokerCard15);
Log.i(TAG, "Selected SharedPref Record UI CardType into Switch: " + cardType);
switch (cardType){
case 1:
[snip]
case 2:
[snip]
case 3:
[snip]
case 4:
[snip]
case 5:
Log.i(TAG, "Selected SharedPref Record Setting UI: " + cardType);
card10.setVisibility(card10.INVISIBLE);
card12.setVisibility(card12.INVISIBLE);
card13.setVisibility(card13.INVISIBLE);
card14.setVisibility(card14.INVISIBLE);
card15.setVisibility(card15.INVISIBLE);
card4TextView.setVisibility(card4TextView.INVISIBLE);
card5TextView.setVisibility(card5TextView.INVISIBLE);
card6TextView.setVisibility(card6TextView.INVISIBLE);
card7TextView.setVisibility(card7TextView.INVISIBLE);
card8TextView.setVisibility(card8TextView.INVISIBLE);
card9TextView.setVisibility(card9TextView.INVISIBLE);
card10TextView.setVisibility(card10TextView.INVISIBLE);
card11TextView.setVisibility(card10TextView.INVISIBLE);
card12TextView.setVisibility(card12TextView.INVISIBLE);
card13TextView.setVisibility(card13TextView.INVISIBLE);
card14TextView.setVisibility(card14TextView.INVISIBLE);
card15TextView.setVisibility(card15TextView.INVISIBLE);
card4img.setImageResource(R.drawable.chihuahua);
card5img.setImageResource(R.drawable.dachshund);
card6img.setImageResource(R.drawable.beagle);
card7img.setImageResource(R.drawable.spaniel);
card8img.setImageResource(R.drawable.boxer);
card9img.setImageResource(R.drawable.shepard);
card11img.setImageResource(R.drawable.dane);
card4img.setVisibility(card4img.VISIBLE);
card5img.setVisibility(card5img.VISIBLE);
card6img.setVisibility(card6img.VISIBLE);
card7img.setVisibility(card7img.VISIBLE);
card8img.setVisibility(card8img.VISIBLE);
card9img.setVisibility(card9img.VISIBLE);
card11img.setVisibility(card11img.VISIBLE);
break;
default:
[snip]
break;
}
}
活动扑克.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="260dp">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="8dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="32dp">
[snip]
<ImageView
android:id="@+id/pokerCard5"
android:layout_width="88dp"
android:layout_height="104dp"
android:layout_marginTop="16dp"
android:background="@color/colorPrimary"
android:contentDescription="@string/localCardSelectcontent"
android:onClick="card8clicked"
app:layout_constraintEnd_toStartOf="@+id/pokerCard6"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/pokerCard4"
app:layout_constraintTop_toBottomOf="@+id/pokerCard2" />
<ImageView
android:id="@+id/pokerCard5img"
android:layout_width="72dp"
android:layout_height="88dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/localCardSelectcontent"
android:onClick="localcard5clicked"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="@+id/pokerCard5"
app:layout_constraintTop_toTopOf="@+id/pokerCard5"
app:srcCompat="@drawable/dachshund" />
<TextView
android:id="@+id/textViewPokerCard5"
android:layout_width="88dp"
android:layout_height="104dp"
android:onClick="localcard5clicked"
android:gravity="center"
android:includeFontPadding="false"
android:textColor="@color/colorPrimaryDark"
android:textSize="44sp"
android:textStyle="normal|bold"
app:layout_constraintStart_toStartOf="@+id/pokerCard5"
app:layout_constraintTop_toTopOf="@+id/pokerCard5" />
<ImageView
android:id="@+id/pokerCard4"
android:layout_width="88dp"
android:layout_height="104dp"
android:background="@color/colorPrimary"
android:contentDescription="@string/localCardSelectcontent"
android:onClick="card5clicked"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="@+id/pokerCard5"
app:layout_constraintEnd_toStartOf="@+id/pokerCard5"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/pokerCard4img"
android:layout_width="72dp"
android:layout_height="88dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/localCardSelectcontent"
android:onClick="localcard4clicked"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="@+id/pokerCard4"
app:layout_constraintTop_toTopOf="@+id/pokerCard4"
app:srcCompat="@drawable/chihuahua" />
<TextView
android:id="@+id/textViewPokerCard4"
android:layout_width="88dp"
android:layout_height="104dp"
android:onClick="localcard4clicked"
android:gravity="center"
android:includeFontPadding="false"
android:textColor="@color/colorPrimaryDark"
android:textSize="44sp"
android:textStyle="normal|bold"
app:layout_constraintStart_toStartOf="@+id/pokerCard4"
app:layout_constraintTop_toTopOf="@+id/pokerCard4" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:onClick="onSwitchChange"
app:headerLayout= "@layout/nav_header_main"
app:menu="@xml/drawer_view" >
</com.google.android.material.navigation.NavigationView>