所以目前我的主屏幕是这样的>
![在此处输入图片描述] [1]
但是,尽管在我的导航抽屉中已经在内部创建了一个开关。因此,因此,我想问一下是否有任何方法可以确保我唯一的开关是导航抽屉中的那个开关?
这是我的layout_switch.xml
文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <Switch android:layout_width="fill_parent" android:layout_height="match_parent" android:id="@+id/btnSwitch"/> </LinearLayout>
这是我的
drawable_menu
文件
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/signIn" android:icon="@drawable/ic_account_circle_black_24dp" android:title="Sign In" /> <item android:id="@+id/Mode" app:actionLayout="@layout/layout_switch" android:icon="@drawable/ic_brightness_4_black_24dp" android:title="Dark Mode" /> </menu>
这是我的
activity_main
文件
<?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:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:id="@+id/drawer" tools:context=".MainActivity"> <include android:layout_height="wrap_content" android:layout_width="match_parent" layout="@layout/drawabletoolbar"/> <include android:layout_height="wrap_content" android:layout_width="match_parent" layout="@layout/content_main"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Switch android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/btnSwitch" android:text=""/> </LinearLayout> <com.google.android.material.navigation.NavigationView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/navi_view" app:menu="@menu/drawable_menu" app:headerLayout="@layout/drawable_header" android:layout_gravity="start" android:fitsSystemWindows="true"/>
这是我的MainActivity文件
package sg.edu.rp.c346.app4thnewspd; import androidx.annotation.RequiresApi; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatDelegate; import androidx.core.view.MenuItemCompat; import androidx.drawerlayout.widget.DrawerLayout; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import androidx.appcompat.widget.Toolbar; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.Switch; import android.widget.Toast; import com.google.android.material.navigation.NavigationView; import java.util.ArrayList; import java.util.List; import java.util.Locale; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import sg.edu.rp.c346.app4thnewspd.Model.Articles; import sg.edu.rp.c346.app4thnewspd.Model.Headlines; public class MainActivity extends AppCompatActivity { RecyclerView recyclerView; SwipeRefreshLayout swipeRefreshLayout; EditText etQuery; Button btnSearch; final String API_KEY = "5353a8d609b4415ab6f449f31d46926a"; Adapter adapter; List<Articles>articles = new ArrayList<>(); private String query; Toolbar toolbar; DrawerLayout drawer; ActionBarDrawerToggle toggle; private Switch btnSwitch; public static final String MYPREFERENCES = "nightModePrefs"; public static final String KEY_ISNIGHTMODE = "isNIghtMode"; SharedPreferences sharedPreferences; @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); drawer = findViewById(R.id.drawer); toggle = new ActionBarDrawerToggle(this,drawer,toolbar,R.string.open,R.string.close); drawer.addDrawerListener(toggle); toggle.setDrawerIndicatorEnabled(true); toggle.syncState(); swipeRefreshLayout = findViewById(R.id.swipeRefresh); recyclerView = findViewById(R.id.recyclerView); etQuery = findViewById(R.id.etQuery); btnSearch = findViewById(R.id.btnSearch); sharedPreferences = getSharedPreferences(MYPREFERENCES, Context.MODE_PRIVATE); btnSwitch = findViewById(R.id.btnSwitch); checkNightModeActivated(); btnSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); saveNightModeState(true); recreate(); }else { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); saveNightModeState(false); recreate(); } } }); recyclerView.setLayoutManager(new LinearLayoutManager(this)); final String country = getCountry(); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { retrieveJson("",country,API_KEY); } }); retrieveJson("",country, API_KEY); btnSearch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!etQuery.getText().toString().equals("")){ swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { retrieveJson(etQuery.getText().toString(),country,API_KEY); } }); retrieveJson(etQuery.getText().toString(),country,API_KEY); }else{ swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { retrieveJson("",country,API_KEY); } }); retrieveJson("",country,API_KEY); } } }); } private void saveNightModeState(boolean nightMode) { SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean(KEY_ISNIGHTMODE,nightMode); editor.apply(); } public void checkNightModeActivated(){ if(sharedPreferences.getBoolean(KEY_ISNIGHTMODE, false)){ btnSwitch.setChecked(true); AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); }else { btnSwitch.setChecked(false); AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); } } private void restartActivity() { Intent i = new Intent(getApplicationContext(),MainActivity.class); startActivity(i); finish(); } private String getCountry() { Locale locale = Locale.getDefault(); String country = locale.getCountry(); return country.toLowerCase(); } public void retrieveJson(final String query, final String country, final String apiKey){ swipeRefreshLayout.setRefreshing(true); Call<Headlines>call; if(!etQuery.getText().toString().equals("")){ call = ApiClient.getInstance().getApi().getSpecificData(query,apiKey); }else{ call = ApiClient.getInstance().getApi().getHeadLines(country, apiKey); } call.enqueue(new Callback<Headlines>() { @Override public void onResponse(Call<Headlines> call, Response<Headlines> response) { if (response.isSuccessful() && response.body().getArticles() != null){ swipeRefreshLayout.setRefreshing(false); articles.clear(); articles = response.body().getArticles(); adapter = new Adapter(MainActivity.this,articles); recyclerView.setAdapter(adapter); } } @Override public void onFailure(Call<Headlines> call, Throwable t) { swipeRefreshLayout.setRefreshing(false); Toast.makeText(MainActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show(); } }); } }
这是我的drawable_header文件
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent" android:layout_height="wrap_content" android:background="#708090" android:orientation="vertical"> <ImageView android:id="@+id/imageView2" android:layout_width="157dp" android:layout_height="137dp" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:elevation="6dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/news" /> <TextView android:id="@+id/textView" android:layout_width="109dp" android:layout_height="108dp" android:layout_marginStart="16dp" android:layout_marginLeft="500dp" android:layout_marginTop="64dp" android:layout_marginEnd="68dp" android:layout_marginRight="68dp" android:text="News" android:textColor="@color/white" android:textSize="25dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/imageView" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
这是我的drawable_toolbar文件
因此,显然现在我可以对我的MainActivity进行一些调整后,将开关切换到暗模式,但是尽管可以使用的开关是您在ss中看到的开关,但我却没有我的导航抽屉。为什么?<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent" android:layout_height="wrap_content" android:background="#708090" android:orientation="vertical"> <ImageView android:id="@+id/imageView2" android:layout_width="157dp" android:layout_height="137dp" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:elevation="6dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/news" /> <TextView android:id="@+id/textView" android:layout_width="109dp" android:layout_height="108dp" android:layout_marginStart="16dp" android:layout_marginLeft="500dp" android:layout_marginTop="64dp" android:layout_marginEnd="68dp" android:layout_marginRight="68dp" android:text="News" android:textColor="@color/white" android:textSize="25dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/imageView" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
*更新
这是我更新后的MainActivity
package sg.edu.rp.c346.app4thnewspd;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.view.MenuItemCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.appcompat.widget.Toolbar;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import sg.edu.rp.c346.app4thnewspd.Model.Articles;
import sg.edu.rp.c346.app4thnewspd.Model.Headlines;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
SwipeRefreshLayout swipeRefreshLayout;
EditText etQuery;
Button btnSearch;
final String API_KEY = "5353a8d609b4415ab6f449f31d46926a";
Adapter adapter;
List<Articles>articles = new ArrayList<>();
private String query;
Toolbar toolbar;
DrawerLayout drawer;
ActionBarDrawerToggle toggle;
private Switch btnSwitch;
public static final String MYPREFERENCES = "nightModePrefs";
public static final String KEY_ISNIGHTMODE = "isNIghtMode";
SharedPreferences sharedPreferences;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
if (AppCompatDelegate.getDefaultNightMode()==AppCompatDelegate.MODE_NIGHT_YES){
setTheme(R.style.DarkTheme);
}else{
setTheme(R.style.LightTheme);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSwitch = findViewById(R.id.btnSwitch);
if(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES){
btnSwitch.setChecked(true);
}
btnSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
restartActivity();
}else{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
restartActivity();
}
}
});
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer);
toggle = new ActionBarDrawerToggle(this,drawer,toolbar,R.string.open,R.string.close);
drawer.addDrawerListener(toggle);
toggle.setDrawerIndicatorEnabled(true);
toggle.syncState();
swipeRefreshLayout = findViewById(R.id.swipeRefresh);
recyclerView = findViewById(R.id.recyclerView);
etQuery = findViewById(R.id.etQuery);
btnSearch = findViewById(R.id.btnSearch);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
final String country = getCountry();
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
retrieveJson("",country,API_KEY);
}
});
retrieveJson("",country, API_KEY);
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!etQuery.getText().toString().equals("")){
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
retrieveJson(etQuery.getText().toString(),country,API_KEY);
}
});
retrieveJson(etQuery.getText().toString(),country,API_KEY);
}else{
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
retrieveJson("",country,API_KEY);
}
});
retrieveJson("",country,API_KEY);
}
}
});
}
private void saveNightModeState(boolean nightMode) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(KEY_ISNIGHTMODE,nightMode);
editor.apply();
}
public void checkNightModeActivated(){
if(sharedPreferences.getBoolean(KEY_ISNIGHTMODE, false)){
btnSwitch.setChecked(true);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}else {
btnSwitch.setChecked(false);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
private void restartActivity() {
Intent i = new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
finish();
}
private String getCountry() {
Locale locale = Locale.getDefault();
String country = locale.getCountry();
return country.toLowerCase();
}
public void retrieveJson(final String query, final String country, final String apiKey){
swipeRefreshLayout.setRefreshing(true);
Call<Headlines>call;
if(!etQuery.getText().toString().equals("")){
call = ApiClient.getInstance().getApi().getSpecificData(query,apiKey);
}else{
call = ApiClient.getInstance().getApi().getHeadLines(country, apiKey);
}
call.enqueue(new Callback<Headlines>() {
@Override
public void onResponse(Call<Headlines> call, Response<Headlines> response) {
if (response.isSuccessful() && response.body().getArticles() != null){
swipeRefreshLayout.setRefreshing(false);
articles.clear();
articles = response.body().getArticles();
adapter = new Adapter(MainActivity.this,articles);
recyclerView.setAdapter(adapter);
}
}
@Override
public void onFailure(Call<Headlines> call, Throwable t) {
swipeRefreshLayout.setRefreshing(false);
Toast.makeText(MainActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
所以目前我的主屏幕是这样的![在此处输入图像描述] [1],尽管在导航抽屉中我已经在其中创建了一个开关。因此,我想问一下...
首先从activity_main布局中删除开关
要解决工具栏新闻重叠,请用以下代码替换content_main.xml。如果解决了,请也接受此答案。