android - PreferenceFragmentCompat & 工具栏问题
问题描述
我正在我的应用程序中使用 PreferenceFragmentCompat,我发现在该片段中显示工具栏时遇到了麻烦。我不擅长在 Android 中编程,所以...
这是 mi preferenceFragmentCompat 的定义。
public class MiCuentaFragment extends PreferenceFragmentCompat {
EditTextPreference usuario;
EditTextPreference clave;
@Override
public void onCreatePreferences(Bundle bundle, String s) {
//addPreferencesFromResource(R.xml.preference_layout);
setPreferencesFromResource(R.xml.preference_layout, s);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
这是他的 XML 的定义
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference/>
<EditTextPreference
android:key="opcion_nombreUsuario"
android:summary="El nombre con el que te identificas dentro de la aplicacion"
android:title="@string/nombre_usuario_opcion" />
<EditTextPreference
android:key="opcion_claveUsuario"
android:summary="Clave que te permite iniciar sesion en la aplicacion"
android:title="@string/clave_opcion" />
<SwitchPreference
android:key="opcion_envioNotificacion"
android:summary="Recibe notificaciones de la aplicacion, noticias e informacion
relevante en tu cuenta de correo"
android:title="@string/notificaciones_opcion"
android:checked="true"/>
</PreferenceScreen>
此外,我在 ActivityMain 中定义了一个 NavigationView 和一个 DrawerLayout,其中一个 FrameLayout 负责加载不同的片段。
我创建了一个 updateView 方法,该方法声明为片段上的接口并在 ActivityMain 中定义,该方法负责根据作为字幕传递的字符串更新工具栏,但在 PreferenceFragment 的情况下,我没有声明的工具栏用 findviewbyid 找到它。
这是 mi MainActivity 布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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:context=".MainActivity"
tools:openDrawer="start">
<FrameLayout
android:id="@+id/container_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/heather_menu_lateral"
app:menu="@menu/menu_lateral" />
</android.support.v4.widget.DrawerLayout>
这是我的 MainActivity.java
public void updateToolbar (String subtitle) {
toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Parking Control");
toolbar.setSubtitle(subtitle);
setSupportActionBar(toolbar);
ActionBarDrawerToggle toogle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
R.string.abrir_menu, R.string.cerrar_menu);
toogle.syncState();
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
Fragment fragment = null;
if (id == R.id.Parqueos) {
//Llama y muestra el fragmento dinamico en el contenedor los registros
getSupportFragmentManager().beginTransaction()
.replace(R.id.container_main, new ParkeosFragment())
.commit();
} else if (id == R.id.MiCuenta) {
//Llamas al Fragmento de la clase de SettingsPreference
getSupportFragmentManager().beginTransaction()
.replace(R.id.container_main, new MiCuentaFragment())
.addToBackStack(null)
.commit();
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
![1]: https://1drv.ms/u/s!Ank6eQEPx3wLhapJODkeaHWigJK1DQ ![2]: https://1drv.ms/u/s!Ank6eQEPx3wLhapKvxJ2H72YGOOAjA
解决方案
您的布局似乎缺少Toolbar
,请尝试以下操作,
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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:context=".MainActivity"
tools:openDrawer="start">
<!-- This LinearLayout represents the contents of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:visibility="visible" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="@+id/container_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/heather_menu_lateral"
app:menu="@menu/menu_lateral" />
</android.support.v4.widget.DrawerLayout>
现在,您可以通过编程方式隐藏/删除不需要的 in 片段,而不是提供Toolbar
in each 。Fragment
Toolbar
推荐阅读
- database - Anylogic Database怎么写在同一行?
- javascript - 为什么 myObj 无法从新对象中识别
- javascript - 向网格函数添加事件侦听器
- python - 使用 python pyqt5 创建单击上下文菜单的事件
- r - 使用 st_as_sf() 绘制等高线在本地 Shiny 中有效,但在 shinyapps.io 上无效
- python - Flask 显示 TypeError: send_from_directory() 缺少 1 个必需的位置参数:'path'
- laravel - Laravel Websockets - 以前工作的 websockets 现在已经停止工作
- python - 如何在 Tkinter 中正确使用 `.grid()`
- java - 对于使用 Maven 的 Spring Boot 应用程序,所有页面都在 Postman 中给出 404 错误
- python - 使用 psycopg2.connect 获取 Postgres 数据库连接的问题,无法转换主机名