[英]How to add ToolBar in PreferenceActivity?
我想在Android應用程序中的PreferenceActivity
中添加ToolBar
。 我寫了以下代碼。
public class SettingsActivity extends PreferenceActivity {
SendSMS sms;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
android.support.v7.widget.Toolbar bar = (android.support.v7.widget.Toolbar) LayoutInflater.from(this).inflate(R.layout.action_bar_setting, root, false);
root.addView(bar, 0);
bar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
}
這在我的機器人Kitkat手機API 19中完美運行,但強制關閉API級別10即姜餅 。 請建議我。
你需要一個包含Toolbar
的布局,以及一個帶有android:id="@android:id/list"
的ListView
activity_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/content_frame"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
SettingsActivity.java
public class SettingsActivity extends PreferenceActivity {
private AppCompatDelegate mDelegate;
@Override
protected void onCreate(Bundle savedInstanceState) {
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
addPreferencesFromResource(R.xml.preferences);
...
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
getDelegate().onPostCreate(savedInstanceState);
}
@Override
public void setContentView(@LayoutRes int layoutResID) {
getDelegate().setContentView(layoutResID);
}
@Override
protected void onPostResume() {
super.onPostResume();
getDelegate().onPostResume();
}
@Override
protected void onStop() {
super.onStop();
getDelegate().onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
getDelegate().onDestroy();
}
private void setSupportActionBar(@Nullable Toolbar toolbar) {
getDelegate().setSupportActionBar(toolbar);
}
private AppCompatDelegate getDelegate() {
if (mDelegate == null) {
mDelegate = AppCompatDelegate.create(this, null);
}
return mDelegate;
}
...
}
看看我的完整工作示例:
來自Android團隊的參考: AppCompatPreferenceActivity
嘗試:
public class SettingsActivity extends AppCompatPreferenceActivity {
.....
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
/* getFragmentManager().beginTransaction()
.replace(android.R.id.content, new GeneralPreferenceFragment())
.commit();
*/
//addPreferencesFromResource(R.xml.pref_general);
}
private void setupActionBar() {
ViewGroup rootView = (ViewGroup)findViewById(R.id.action_bar_root); //id from appcompat
if (rootView != null) {
View view = getLayoutInflater().inflate(R.layout.app_bar_layout, rootView, false);
rootView.addView(view, 0);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
app_bar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
您可以從@android:style/Theme.Material.Light.DarkActionBar
輕松添加工具欄
在AndroidManifest.xml中 :
<activity
android:name=".activity.SettingsActivity"
android:theme="@style/SettingsTheme"
android:label="Settings"/>
在v21 / styles.xml中
<style name="SettingsTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
<item name="android:colorPrimary">@color/colorPrimary</item>
<item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
在v14 / styles.xml中,用於Back API支持
<style name="SettingsTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/ActionBar.V14.Movie.NoTitle</item>
在我的情況下通過膨脹自定義工具欄非常容易和運作良好。 在你的java代碼中做如下,
public class SettingsPrefActivity extends AppCompatPreferenceActivity {
// private static final String TAG = SettingsPrefActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setting up toolbar
getLayoutInflater().inflate(R.layout.toolbar_setting, (ViewGroup) findViewById(android.R.id.content));
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Settings");
setSupportActionBars(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// load settings fragment
getFragmentManager().beginTransaction().replace(android.R.id.content, new MainPreferenceFragment()).commit();
}
}
在您的xml端代碼中,在頂部添加一個首選項類別,如下所示,
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
//put below line at the top of your xml preference layout screen..
<PreferenceCategory android:layout="@layout/toolbar_setting"></PreferenceCategory>
並在您的布局資源文件夾toolbar_setting中,
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="@dimen/appbar_elevation"
android:minHeight="?attr/actionBarSize"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
我遲到了,但這是對問題的一個小修復,而不是編寫大量的代碼或添加龐大的庫。
轉到AndroidManifest.xml
並在Preference Activity中添加自定義theme
<activity
android:name=".Dashboard.PreferencepActivity"
android:label="@string/title_activity_preference"
android:theme="@style/Pref"></activity>
這是上面添加到@style/Pref
的自定義主題
<style name="Pref" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
希望能幫助到你!
代替:
public class PreferencesActivity extends Activity
做這個:
public class PreferencesActivity extends AppCompatActivity
此方法不會解決添加工具欄的問題,但您可以為設置頁面返回默認的ActionBar
。 通過從父主題繼承,為設置活動創建主題。
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar.Bridge">
<!-- Customize your theme here. -->
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.Settings" parent="AppTheme">
<item name="windowNoTitle">false</item>
<item name="windowActionBar">true</item>
</style>
在AndroidManifest.xml中設置android:theme
<activity
android:name=".Settings"
android:theme="@style/AppTheme.Settings" />
就這些。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.