簡體   English   中英

操作欄未顯示:首選項活動中的Theme.Appcompat.Light.DarkActionBar

[英]Action bar not showing :Theme.Appcompat.Light.DarkActionBar in preference activity

我想在我的偏好活動中使用這個主題,因為好的復選框但我的動作欄沒有顯示。

如果您不想在ActionBarActivity使用PreferenceFragment並且仍希望將現有的PreferenceActivityAppCompat支持庫v7一起使用,則可以使用Toolbar並使用自定義布局覆蓋PreferenceActvity.setContentView()

您可以在保持當前PreferenceActivity同時使用Toolbar支持,它適用於2.3.4及更高版本

private Toolbar mActionBar;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.Theme_MyApp_Settings);

    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.settings);

    mActionBar.setTitle(getTitle());

    //other things to create/init 
}


public void setContentView(int layoutResID) {
    ViewGroup contentView = (ViewGroup) LayoutInflater.from(this).inflate(
        R.layout.settings_activity, new LinearLayout(this), false);

    mActionBar = (Toolbar) contentView.findViewById(R.id.action_bar);
    mActionBar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    ViewGroup contentWrapper = (ViewGroup) contentView.findViewById(R.id.content_wrapper);
    LayoutInflater.from(this).inflate(layoutResID, contentWrapper, true);

    getWindow().setContentView(contentView);
}

settings_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/action_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize" />

    <FrameLayout
        android:id="@+id/content_wrapper"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

styles.xml

<style name="Theme.MyApp.Settings" parent="@style/Theme.AppCompat">
    <item name="android:windowNoTitle">true</item>
    <item name="toolbarStyle">@style/Widget.Toolbar</item>
</style>


<style name="Widget.Toolbar" parent="@style/Widget.AppCompat.Toolbar">
    <item name="android:background">?attr/colorPrimary</item>
    <item name="navigationIcon">?attr/homeAsUpIndicator</item>
</style>

PreferenceActivity不會擴展ActionBarActivity ,這是操作欄可用所必需的。

如果您需要帶有操作欄的首選項屏幕,請嘗試在ActionBarActivity使用PreferenceFragment

public class SettingsActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new SettingsFragment())
                .commit();
    }
}

public static class SettingsFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
    }
    ...
}

參考: http//developer.android.com/guide/topics/ui/settings.html#Fragment

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM