2017-07-11 9 views
0

私はAndroidにはとても新しいので、私のPreferenceScreenアクティビティのヘッダを表示するのに問題があります。私は私のクラスの宣言は、このAndroidの `PreferenceScreen`のヘッダー/タイトルがありません

@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> 
のようなこの

public class SettingsActivity extends PreferenceActivity 

onCreateようになり、この

enter image description here

のようなタイトルを表示したいです私は今、何を得る

は、私はすでに無駄にPreferenceActivity上android:title="My Settings"setTitleを試してみましたが、この

enter image description here

です。どんな助けもありがとう。タイトルはそれに表示されbecauase

+0

ます。https:/ /stackoverflow.com/a/6714971/6050536 – Ali

+0

@Ali私はそれを試みた、それはworではない王のどちらか:-( –

答えて

0

変更PreferenceActivityがAppCompatPretenceActivity

public class SettingsActivity extends PreferenceActivity 

にあなたは、このようにアクションバーを設定する必要があります。

@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(thi ...'がjava.lang.ClassCastExceptionをスローしています:android.support.v7.widget.Toolbarをandroid.widgetにキャストできません。私はAndroid 6.0.1上にいる –

関連する問題