我正在使用appcompat-v7:23.0.1和support-v4:23.0.1
在使用AppCompayPreferenceActivity之前,我的PreferenceActivity在没有工具栏的情况下查看以下内容.
没有工具栏
public class JstockPreferenceActivity extends PreferenceActivity { public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState == null) { // display the fragment as the main content. JstockPreferenceFragment JstockPreferenceFragment = JstockPreferenceFragment.newInstance(); JstockPreferenceFragment.setArguments(this.getIntent().getExtras()); this.getFragmentManager().beginTransaction().replace(androID.R.ID.content,JstockPreferenceFragment).commit(); }}
使用AppCompayPreferenceActivity后,使用一些工具栏初始化代码,结果如下所示
结果并不像预期的那样: –
>工具栏与PreferenceFragment重叠
>在工具栏上触摸时,PreferenceFragment将使用该事件.这意味着,当您触摸工具栏时,将触摸“表格标题”首选项.
使用工具栏
public class JstockPreferenceActivity extends AppCompatPreferenceActivity { public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState == null) { // display the fragment as the main content. JstockPreferenceFragment JstockPreferenceFragment = JstockPreferenceFragment.newInstance(); JstockPreferenceFragment.setArguments(this.getIntent().getExtras()); this.getFragmentManager().beginTransaction().replace(androID.R.ID.content,JstockPreferenceFragment).commit(); } initToolbar(); } private voID initToolbar() { Toolbar toolbar; if (androID.os.Build.VERSION.SDK_INT >= androID.os.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.toolbar,root,false); root.addVIEw(toolbar,0); } else { toolbar = null; } setSupportActionbar(toolbar); getSupportActionbar().setdisplayHomeAsUpEnabled(true); }}
我的工具栏是
toolbar.xml
<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/toolbar" androID:layout_height="wrap_content" androID:layout_wIDth="match_parent" androID:minHeight="?attr/actionbarSize" androID:background="?attr/colorPrimary" androID:elevation="4dp" app:theme="@style/themeOverlay.AppCompat.Dark.Actionbar" app:popuptheme="@style/themeOverlay.AppCompat.light" > <!-- androID:elevation="4dp" is used due to http://www.Google.com/design/spec/what-is-material/elevation-shadows.HTML#elevation-shadows-elevation-androID- --></androID.support.v7.Widget.Toolbar>
我想知道,我做错了什么,这导致Toolbar与PreferenceFragment重叠
解决方法 经过一些研究并从 Ian Lake获得建议后,我设法使其成功>从AppCompatPreferenceActivity更改为AppCompatActivity PreferenceFragmentCompat
以下解决方案适用于API 10及更高版本.
JstockPreferenceActivity.java
public class JstockPreferenceActivity extends AppCompatActivity { @SuppressWarnings("deprecation") @Suppresslint("NewAPI") @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.Jstock_preference_activity); initToolbar(); if (savedInstanceState == null) { // display the fragment as the main content. JstockPreferenceFragment JstockPreferenceFragment = JstockPreferenceFragment.newInstance(); JstockPreferenceFragment.setArguments(this.getIntent().getExtras()); this.getSupportFragmentManager().beginTransaction().replace(R.ID.content,JstockPreferenceFragment).commit(); } }
JstockPreferenceFragment.java
public class JstockPreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferencechangelistener,PreferenceFragmentCompat.OnPreferencedisplayDialogCallback { public static JstockPreferenceFragment newInstance() { return new JstockPreferenceFragment(); }
但是,这样做有一个缺点.您之前的自定义DialogPreference不再有效.要解决这个问题,你需要
>将DialogPreference替换为PreferenceDialogFragmentCompat.
>使用ListPreference替换XML中的DialogPreference. (我不确定ListPreference是否是一种正确的替代方法.无论如何都可以.)
>在PreferenceFragmentCompat的onPreferencedisplayDialog中手动显示PreferenceDialogFragmentCompat.
JstockPreferenceFragment.java
public class JstockPreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferencechangelistener,PreferenceFragmentCompat.OnPreferencedisplayDialogCallback { public static JstockPreferenceFragment newInstance() { return new JstockPreferenceFragment(); } //////////////////////////////////////////////////////////////////////////////////////////////// // Showing custom dialog preference. private voID showPreferenceDialogFragmentCompat(PreferenceDialogFragmentCompat preferenceDialogFragmentCompat) { preferenceDialogFragmentCompat.setTargetFragment(this,0); preferenceDialogFragmentCompat.show(this.getFragmentManager(),"androID.support.v7.preference.PreferenceFragment.DIALOG"); } private voID showCurrencySymbolPreferenceDialogFragmentCompat(Preference preference) { CurrencySymbolPreferenceDialogFragmentCompat currencySymbolPreferenceDialogFragmentCompat = CurrencySymbolPreferenceDialogFragmentCompat.newInstance(preference.getKey()); showPreferenceDialogFragmentCompat(currencySymbolPreferenceDialogFragmentCompat); } // Showing custom dialog preference. //////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////// // Callback when we are using custom dialog preference. @OverrIDe public Fragment getCallbackFragment() { return this; } @OverrIDe public boolean onPreferencedisplayDialog(PreferenceFragmentCompat preferenceFragmentCompat,Preference preference) { final String key = preference.getKey(); if (_CURRENCY_SYMBol_OPTIONS.equals(key)) { showCurrencySymbolPreferenceDialogFragmentCompat(preference); return true; } return false; } // Callback when we are using custom dialog preference. ////////////////////////////////////////////////////////////////////////////////////////////////
CurrencySymbolPreferenceDialogFragmentCompat.java
public class CurrencySymbolPreferenceDialogFragmentCompat extends PreferenceDialogFragmentCompat { public CurrencySymbolPreferenceDialogFragmentCompat() { } public static CurrencySymbolPreferenceDialogFragmentCompat newInstance(String key) { CurrencySymbolPreferenceDialogFragmentCompat fragment = new CurrencySymbolPreferenceDialogFragmentCompat(); Bundle b = new Bundle(1); b.putString("key",key); fragment.setArguments(b); return fragment; } @OverrIDe public voID onDialogClosed(boolean positiveResult) { if (positiveResult) { ... this.getPreference().setSummary(getSummary()); ... } } }
的preferences.xml
<androID.support.v7.preference.ListPreference androID:title="@string/preference_currency_symbol_Title" androID:key="_CURRENCY_SYMBol_OPTIONS" />
请注意,您需要在主题中添加以下项目.
<item name="preferencetheme">@style/PreferencethemeOverlay</item>
关于主题的一些错误
然而,主题还不完美 – PreferenceFragmentCompat requires preferenceTheme to be set
这是一个众所周知的问题:https://code.google.com/p/android/issues/detail?id=183376
建议的解决方法https://stackoverflow.com/a/32108439/72437但是,解决方法仅适用于v14及更高版本,而不适用于v7及更高版本.
总结以上是内存溢出为你收集整理的android – 通过AppCompatPreferenceActivity将工具栏添加到PreferenceActivity会导致重叠全部内容,希望文章能够帮你解决android – 通过AppCompatPreferenceActivity将工具栏添加到PreferenceActivity会导致重叠所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)