我想在我的偏好 Activity 中使用这个主题,因为它有一个漂亮的复选框,但我的操作栏没有显示。
最佳答案
如果您不想在 ActionBarActivity
中使用 PreferenceFragment
并且仍想将现有的 PreferenceActivity
与 AppCompat< 一起使用
支持库 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>
样式.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>
关于android - 操作栏未在首选项 Activity 中显示 :Theme. Appcompat.Light.DarkActionBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28933675/