Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当NoActionBar被设置为应用程序主题时,如何向Android PreferenceActivity添加工具栏?_Android_Android Activity_Kotlin_Android Fragmentactivity - Fatal编程技术网

当NoActionBar被设置为应用程序主题时,如何向Android PreferenceActivity添加工具栏?

当NoActionBar被设置为应用程序主题时,如何向Android PreferenceActivity添加工具栏?,android,android-activity,kotlin,android-fragmentactivity,Android,Android Activity,Kotlin,Android Fragmentactivity,我有以下问题。我想在Android Studio生成的preferencesActivity中添加一个工具栏,但我使用NoActionBar作为我的应用程序主题(因为我的活动中有一个自定义工具栏) 生成的首选项活动没有布局文件,我无法在其中添加该工具栏。有没有办法设置它呢?(在这种情况下,即使是标准工具栏也可以工作,因为我只需要返回按钮和标题即可显示) 优先性 PACKAGENAME 导入android.annotation.TargetApi 导入android.content.Context

我有以下问题。我想在Android Studio生成的
preferencesActivity
中添加一个工具栏,但我使用
NoActionBar
作为我的应用程序主题(因为我的活动中有一个自定义工具栏)

生成的首选项活动没有布局文件,我无法在其中添加该工具栏。有没有办法设置它呢?(在这种情况下,即使是标准工具栏也可以工作,因为我只需要返回按钮和标题即可显示)

优先性
PACKAGENAME
导入android.annotation.TargetApi
导入android.content.Context
导入android.content.Intent
导入android.content.res.Configuration
导入android.media.RingtoneManager
导入android.net.Uri
导入android.os.Build
导入android.os.Bundle
导入android.preference.ListPreference
导入android.preference.preference
导入android.preference.PreferenceActivity
导入android.preference.PreferenceFragment
导入android.preference.PreferenceManager
导入android.preference.RingtonePreference
导入android.text.TextUtils
导入android.view.MenuItem
导入android.support.v4.app.NavUtils
进口迪莱罗快速公园
/**
*显示一组应用程序设置的[PreferenceActivity]。在…上
*手机设备、设置以单个列表的形式显示。在平板电脑上,
*设置按类别拆分,类别标题显示在
*设置列表。
*
*请参阅[Android设计:设置](http://developer.android.com/design/patterns/settings.html)
*有关设计指南和[设置API指南](http://developer.android.com/guide/topics/ui/settings.html)
*有关开发设置UI的详细信息。
*/
类设置活动:AppCompatingPreferenceActivity(){
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setupActionBar()
}
/**
*如果API可用,请设置[android.app.ActionBar]。
*/
私人娱乐设置ActionBar(){
supportActionBar?.setDisplayHomeAsUpEnabled(真)
}
覆盖选定的菜单项(featureId:Int,item:MenuItem):布尔值{
val id=item.itemId
if(id==android.R.id.home){
如果(!super.onMenuItemSelected(featureId,item)){
NavUtils.navigateUpFromSameTask(本)
}
返回真值
}
返回super.onMenuItemSelected(featureId,item)
}
/**
*{@inheritardoc}
*/
覆盖多窗格():布尔值{
返回ISXLarge平板电脑(此)
}
/**
*{@inheritardoc}
*/
@TargetApi(构建版本代码蜂窝)
覆盖BuildHeaders(目标:列表){
loadHeadersFromResource(R.xml.pref_头,目标)
}
/**
*此方法停止恶意应用程序中的片段注入。
*确保在这里拒绝任何未知片段。
*/
override fun isValidFragment(fragmentName:String):布尔值{
返回PreferenceFragment::class.java.name==fragmentName
||GeneralReferenceFragment::class.java.name==碎片名称
||DataSyncPreferenceFragment::class.java.name==fragmentName
||NotificationPreferenceFragment::class.java.name==fragmentName
}
/**
*此片段仅显示一般首选项。当
*“活动”显示了一个双窗格设置UI。
*/
@TargetApi(构建版本代码蜂窝)
类GeneralReferenceFragment:PreferenceFragment(){
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
addPreferencesFromResource(R.xml.pref_-general)
设置选项菜单(真)
//绑定编辑文本/列表/对话框/铃声首选项的摘要
//当他们的价值观发生变化时,他们的总结
//根据Android设计,更新以反映新的价值
//准则。
bindPreferenceSummaryToValue(FindReference(“示例文本”))
bindPreferenceSummaryToValue(FindReference(“示例列表”))
}
覆盖选项ItemSelected(项:菜单项):布尔值{
val id=item.itemId
if(id==android.R.id.home){
startActivity(意图(活动,设置活动::class.java))
返回真值
}
返回super.onOptionsItemSelected(项目)
}
}
/**
*此片段仅显示通知首选项。当
*“活动”显示了一个双窗格设置UI。
*/
@TargetApi(构建版本代码蜂窝)
类NotificationPreferenceFragment:PreferenceFragment(){
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
addPreferencesFromResource(R.xml.pref_通知)
设置选项菜单(真)
//绑定编辑文本/列表/对话框/铃声首选项的摘要
//当他们的价值观发生变化时,他们的总结
//根据Android设计,更新以反映新的价值
//准则。
bindPreferenceSummaryToValue(FindReference(“通知\新消息\铃声”))
}
覆盖选项ItemSelected(项:菜单项):布尔值{
val id=item.itemId
if(id==android.R.id.home){
startActivity(意图(活动,设置活动::class.java))
返回真值
}
返回super.onOptionsItemSelected(项目)
}
}
/**
*此片段仅显示数据和同步首选项。当
*“活动”显示了一个双窗格设置UI。
*/
@TargetApi(构建版本代码蜂窝)
类DataSyncPreferenceFragment:PreferenceFragment(){
重写
package PACKAGENAME

import android.annotation.TargetApi
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.media.RingtoneManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.preference.ListPreference
import android.preference.Preference
import android.preference.PreferenceActivity
import android.preference.PreferenceFragment
import android.preference.PreferenceManager
import android.preference.RingtonePreference
import android.text.TextUtils
import android.view.MenuItem
import android.support.v4.app.NavUtils
import de.dilello.quickpark.R

/**
 * A [PreferenceActivity] that presents a set of application settings. On
 * handset devices, settings are presented as a single list. On tablets,
 * settings are split by category, with category headers shown to the left of
 * the list of settings.
 *
 * See [Android Design: Settings](http://developer.android.com/design/patterns/settings.html)
 * for design guidelines and the [Settings API Guide](http://developer.android.com/guide/topics/ui/settings.html)
 * for more information on developing a Settings UI.
 */
class SettingsActivity : AppCompatPreferenceActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setupActionBar()
    }

    /**
     * Set up the [android.app.ActionBar], if the API is available.
     */
    private fun setupActionBar() {
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
    }

    override fun onMenuItemSelected(featureId: Int, item: MenuItem): Boolean {
        val id = item.itemId
        if (id == android.R.id.home) {
            if (!super.onMenuItemSelected(featureId, item)) {
                NavUtils.navigateUpFromSameTask(this)
            }
            return true
        }
        return super.onMenuItemSelected(featureId, item)
    }

    /**
     * {@inheritDoc}
     */
    override fun onIsMultiPane(): Boolean {
        return isXLargeTablet(this)
    }

    /**
     * {@inheritDoc}
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    override fun onBuildHeaders(target: List<PreferenceActivity.Header>) {
        loadHeadersFromResource(R.xml.pref_headers, target)
    }

    /**
     * This method stops fragment injection in malicious applications.
     * Make sure to deny any unknown fragments here.
     */
    override fun isValidFragment(fragmentName: String): Boolean {
        return PreferenceFragment::class.java.name == fragmentName
                || GeneralPreferenceFragment::class.java.name == fragmentName
                || DataSyncPreferenceFragment::class.java.name == fragmentName
                || NotificationPreferenceFragment::class.java.name == fragmentName
    }

    /**
     * This fragment shows general preferences only. It is used when the
     * activity is showing a two-pane settings UI.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    class GeneralPreferenceFragment : PreferenceFragment() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            addPreferencesFromResource(R.xml.pref_general)
            setHasOptionsMenu(true)

            // Bind the summaries of EditText/List/Dialog/Ringtone preferences
            // to their values. When their values change, their summaries are
            // updated to reflect the new value, per the Android Design
            // guidelines.
            bindPreferenceSummaryToValue(findPreference("example_text"))
            bindPreferenceSummaryToValue(findPreference("example_list"))
        }

        override fun onOptionsItemSelected(item: MenuItem): Boolean {
            val id = item.itemId
            if (id == android.R.id.home) {
                startActivity(Intent(activity, SettingsActivity::class.java))
                return true
            }
            return super.onOptionsItemSelected(item)
        }
    }

    /**
     * This fragment shows notification preferences only. It is used when the
     * activity is showing a two-pane settings UI.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    class NotificationPreferenceFragment : PreferenceFragment() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            addPreferencesFromResource(R.xml.pref_notification)
            setHasOptionsMenu(true)

            // Bind the summaries of EditText/List/Dialog/Ringtone preferences
            // to their values. When their values change, their summaries are
            // updated to reflect the new value, per the Android Design
            // guidelines.
            bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"))
        }

        override fun onOptionsItemSelected(item: MenuItem): Boolean {
            val id = item.itemId
            if (id == android.R.id.home) {
                startActivity(Intent(activity, SettingsActivity::class.java))
                return true
            }
            return super.onOptionsItemSelected(item)
        }
    }

    /**
     * This fragment shows data and sync preferences only. It is used when the
     * activity is showing a two-pane settings UI.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    class DataSyncPreferenceFragment : PreferenceFragment() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            addPreferencesFromResource(R.xml.pref_data_sync)
            setHasOptionsMenu(true)

            // Bind the summaries of EditText/List/Dialog/Ringtone preferences
            // to their values. When their values change, their summaries are
            // updated to reflect the new value, per the Android Design
            // guidelines.
            bindPreferenceSummaryToValue(findPreference("sync_frequency"))
        }

        override fun onOptionsItemSelected(item: MenuItem): Boolean {
            val id = item.itemId
            if (id == android.R.id.home) {
                startActivity(Intent(activity, SettingsActivity::class.java))
                return true
            }
            return super.onOptionsItemSelected(item)
        }
    }

    companion object {

        /**
         * A preference value change listener that updates the preference's summary
         * to reflect its new value.
         */
        private val sBindPreferenceSummaryToValueListener = Preference.OnPreferenceChangeListener { preference, value ->
            val stringValue = value.toString()

            if (preference is ListPreference) {
                // For list preferences, look up the correct display value in
                // the preference's 'entries' list.
                val listPreference = preference
                val index = listPreference.findIndexOfValue(stringValue)

                // Set the summary to reflect the new value.
                preference.setSummary(
                        if (index >= 0)
                            listPreference.entries[index]
                        else
                            null)

            } else if (preference is RingtonePreference) {
                // For ringtone preferences, look up the correct display value
                // using RingtoneManager.
                if (TextUtils.isEmpty(stringValue)) {
                    // Empty values correspond to 'silent' (no ringtone).
                    preference.setSummary(R.string.pref_ringtone_silent)

                } else {
                    val ringtone = RingtoneManager.getRingtone(
                            preference.getContext(), Uri.parse(stringValue))

                    if (ringtone == null) {
                        // Clear the summary if there was a lookup error.
                        preference.setSummary(null)
                    } else {
                        // Set the summary to reflect the new ringtone display
                        // name.
                        val name = ringtone.getTitle(preference.getContext())
                        preference.setSummary(name)
                    }
                }

            } else {
                // For all other preferences, set the summary to the value's
                // simple string representation.
                preference.summary = stringValue
            }
            true
        }

        /**
         * Helper method to determine if the device has an extra-large screen. For
         * example, 10" tablets are extra-large.
         */
        private fun isXLargeTablet(context: Context): Boolean {
            return context.resources.configuration.screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK >= Configuration.SCREENLAYOUT_SIZE_XLARGE
        }

        /**
         * Binds a preference's summary to its value. More specifically, when the
         * preference's value is changed, its summary (line of text below the
         * preference title) is updated to reflect the value. The summary is also
         * immediately updated upon calling this method. The exact display format is
         * dependent on the type of preference.

         * @see .sBindPreferenceSummaryToValueListener
         */
        private fun bindPreferenceSummaryToValue(preference: Preference) {
            // Set the listener to watch for value changes.
            preference.onPreferenceChangeListener = sBindPreferenceSummaryToValueListener

            // Trigger the listener immediately with the preference's
            // current value.
            sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
                    PreferenceManager
                            .getDefaultSharedPreferences(preference.context)
                            .getString(preference.key, ""))
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:contentInsetStart="0dp"
    android:contentInsetLeft="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:theme="@style/ThemeOverlay.AppCompat.Dark"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:titleTextColor="@android:color/white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/iv_menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:src="@drawable/ic_menu"
            />

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textColor="@android:color/white"
            android:textSize="@dimen/toolbar_title_size"
            android:gravity="center"
            android:text="MDS Manager"
            android:singleLine="true"
            android:typeface="monospace"
            />

    </LinearLayout>
</android.support.v7.widget.Toolbar>

</LinearLayout>
<LinearLayout 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:orientation="vertical"
tools:context="com.mds.manager.mdsmanager.activities.MainActivity">

<include
    layout="@layout/toolbar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:typeface">serif</item>
</style>

</resources>