2017-07-11 29 views
0

我對Android非常陌生,並且無法顯示我的PreferenceScreen活動的標頭。我要顯示這樣安卓中「PreferenceScreen」的標題/標題缺失

enter image description here

我的類聲明是這樣的

public class SettingsActivity extends PreferenceActivity 

onCreate這樣

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    _settingsFragment = new SettingsFragment(); 

    getFragmentManager().beginTransaction().replace(android.R.id.content, _settingsFragment).commit(); 
} 

的preferences.xml

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 
    <Preference 
     android:key="pref_key_vault_storage" 
     android:title="Vault Storage" 
     android:summary="Set/modify the vault storage location." > 
     <intent 
      android:targetPackage="com.test.concealx" 
      android:targetClass="com.test.concealx.ChooseStorageActivity"/> 
    </Preference> 
    <EditTextPreference 
     android:id="@+id/id_path" 
     android:title="Vault Path" 
     android:key="pref_key_vault_path" 
     android:summary="Set/modify the storage path. DO NOT change this unless you are sure about it."> 
    </EditTextPreference> 
</PreferenceScreen> 
標題

我現在得到的是這種

enter image description here

我已經在PreferenceActivity無濟於事試圖android:title="My Settings"setTitle。任何幫助,將不勝感激。

+0

HTTPS:/ /stackoverflow.com/a/6714971/6050536 – Ali

+0

@Ali我試過了,它不是工作國王之一:-( –

回答

0

變化PreferenceActivity到AppCompatPretenceActivity

public class SettingsActivity extends PreferenceActivity 

您需要設置動作條這樣becauase顯示在其標題:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    _settingsFragment = new SettingsFragment(); 

    getFragmentManager().beginTransaction().replace(android.R.id.content, _settingsFragment).commit(); 


    Toolbar toolbar; 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 
      ViewGroup root = (ViewGroup) findViewById(android.R.id.list).getParent().getParent().getParent(); 
      toolbar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false); 
      root.addView(toolbar, 0); 
     } else { 
      ViewGroup root = (ViewGroup) findViewById(android.R.id.content); 
      ListView content = (ListView) root.getChildAt(0); 
      root.removeAllViews(); 
      toolbar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false); 
      int height; 
      TypedValue tv = new TypedValue(); 
      if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) { 
       height = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics()); 
      } else { 
       height = toolbar.getHeight(); 
      } 
      content.setPadding(0, height, 0, 0); 
      root.addView(content); 
      root.addView(toolbar); 
     } 
     setSupportActionBar(toolbar); 

} 

settings_toolbar.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolbar2" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?attr/actionBarSize" 
    app:navigationContentDescription="@string/abc_action_bar_up_description" 
    android:background="?attr/colorPrimary" 
    /> 
+0

這隻會顯示爲一個組標題,而不是主要/活動級別標題 –

+0

現在試試這個。創建一個工具欄並將其設置爲ActionBar – Ali

+0

'toolbar =(Toolbar)LayoutInflater.from(此處不提示'java.lang.ClassCastException:android.support.v7.widget.Toolbar不能轉換爲android.widget。工具欄「。我在Android 6.0.1上 –