Tex*_*xts 5 android android-preferences android-actionbar android-toolbar
我正在尝试通过工具栏使用 Android Jetpack设置指南。该指南说根标签是 <PreferencesScreen>,所以我不能在 xml 中包含工具栏。我正在使用NoActionBar主题。根据 Android Jetpack支持应用栏变体指南,建议从 Activity 中删除顶部栏,并在每个目标片段中定义它。所以我有以下文件:
首选项.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<SwitchPreferenceCompat
app:key="notifications"
app:title="Enable message notifications" />
<Preference
app:key="feedback"
app:summary="Report technical issues or suggest new features"
app:title="Send feedback" />
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)
设置Fragment.kt
class SettingsFragment: PreferenceFragmentCompat(){
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
}
}
Run Code Online (Sandbox Code Playgroud)
一切运行良好,并且 settingsFragments 打开得很好,但因为我确实使用 NoActionBar 主题,所以似乎没有办法在 main_activity 中定义工具栏的情况下添加工具栏。我的评估是否正确,或者有没有办法让我在首选项片段中添加自定义工具栏?
小智 10
也许为时已晚,但你可以通过这种方式实现 ->
<style name="PrefsTheme" parent="PreferenceThemeOverlay.v14.Material">
<item name="android:layout">@layout/fragment_settings</item>
</style>
Run Code Online (Sandbox Code Playgroud)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/item_toolbar" />
<!-- the id is defined by the support lib. -->
<FrameLayout
android:id="@android:id/list_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:targetApi="n" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="preferenceTheme">@style/PrefsTheme</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后在您的 SettingsFragment 中,您可以通过 id 找到工具栏并对其进行所有自定义:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Toolbar>(R.id.toolbar)?.let {
// Customize toolbar
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1080 次 |
| 最近记录: |