问题描述
我已通过不见了
我用应用程序兼容性-V7:23.0.1
和支持-V4:23.0.1
使用前 AppCompay preferenceActivity
,我的 preferenceActivity
看起来没有工具栏下面。
无工具栏
公共类JStock preferenceActivity扩展$ P $ {pferenceActivity 公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 如果(savedInstanceState == NULL){
//显示片断为主要内容。
JStock preferenceFragment jStock preferenceFragment = JStock preferenceFragment.newInstance();
jStock preferenceFragment.setArguments(this.getIntent()getExtras());
。this.getFragmentManager()调用BeginTransaction()取代(android.R.id.content,jStock preferenceFragment).commit()。
}
}
使用后 AppCompay preferenceActivity
,有些工具栏
初始化code,结果看起来以下
未如预期的结果: -
- 的
工具栏
与preferenceFragment
重叠 - 当触及
工具栏
,该事件将被preferenceFragment
消耗。这意味着,当你触摸工具栏上的表头preference将被替代的感动。
使用工具栏
公共类JStock preferenceActivity扩展应用程序兼容性$ P $ {pferenceActivity
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 如果(savedInstanceState == NULL){
//显示片断为主要内容。
JStock preferenceFragment jStock preferenceFragment = JStock preferenceFragment.newInstance();
jStock preferenceFragment.setArguments(this.getIntent()getExtras());
。this.getFragmentManager()调用BeginTransaction()取代(android.R.id.content,jStock preferenceFragment).commit()。
}
initToolbar();
} 私人无效initToolbar(){
工具栏工具栏; 如果(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()。
工具栏=(栏)LayoutInflater.from(本).inflate(R.layout.toolbar,根,假);
root.addView(工具栏,0);
}其他{
工具栏= NULL;
} setSupportActionBar(工具栏);
getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
}
}
我的工具栏
toolbar.xml
< android.support.v7.widget.Toolbar的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
机器人:ID =@ + ID /工具栏
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =match_parent
安卓了minHeight =?ATTR / actionBarSize
机器人:ATTR / colorPrimary背景=
机器人:海拔=4DP
应用:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar
应用:popupTheme =@风格/ ThemeOverlay.AppCompat.Light> < - 机器人:海拔=4DP用于因http://www.google.com/design/spec/what-is-material/elevation-shadows.html#elevation-shadows-elevation-android- - >< /android.support.v7.widget.Toolbar>
我想知道,我有什么做错了,这会导致工具栏
重叠 preferenceFragment
经过一番研究,并从的,我设法让它通过。
- 从
应用程序兼容性preferenceActivity
更改,以AppCompatActivity
+preferenceFragmentCompat
下面的解决方案是可行的API 10以上。
JStock preferenceActivity.java
公共类JStock preferenceActivity延伸AppCompatActivity { @燮pressWarnings(德precation)
@燮pressLint(NewApi)
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 的setContentView(R.layout.jstock_ preference_activity); initToolbar(); 如果(savedInstanceState == NULL){
//显示片断为主要内容。
JStock preferenceFragment jStock preferenceFragment = JStock preferenceFragment.newInstance();
jStock preferenceFragment.setArguments(this.getIntent()getExtras());
。this.getSupportFragmentManager()调用BeginTransaction()取代(R.id.content,jStock preferenceFragment).commit()。
}
}
JStock preferenceFragment.java
公共类JStock preferenceFragment扩展preferenceFragmentCompat实现共享preferences.OnShared preferenceChangeListener,preferenceFragmentCompat.On $ P $ {pferenceDisplayDialogCallback 公共静态JStock preferenceFragment的newInstance(){
返回新JStock preferenceFragment();
}
然而,通过这样做,有一个缺点。您的previous定制对话preference
不再起作用。为了解决这个问题,需要
- 替换
对话preference
与preferenceDialogFragmentCompat
。 - 替换
对话preference
在XML与目录preference
。 (我不能完全确定是否目录preference
作为替代是一个正确的做法。它的工作原理是这样) - 手动显示
preferenceDialogFragmentCompat
在preferenceFragmentCompat
的上preferenceDisplayDialog
。
JStock preferenceFragment.java
公共类JStock preferenceFragment扩展preferenceFragmentCompat实现共享preferences.OnShared preferenceChangeListener,preferenceFragmentCompat.On $ P $ {pferenceDisplayDialogCallback 公共静态JStock preferenceFragment的newInstance(){
返回新JStock preferenceFragment();
} ////////////////////////////////////////////////// //////////////////////////////////////////////
//显示定制对话框preference。
私人无效秀preferenceDialogFragmentCompat(preferenceDialogFragmentCompat preferenceDialogFragmentCompat){
preferenceDialogFragmentCompat.setTargetFragment(此,0);
preferenceDialogFragmentCompat.show(this.getFragmentManager(),android.support.v7 preference preferenceFragment.DIALOG。);
} 私人无效showCurrencySymbol preferenceDialogFragmentCompat(preference preference){
CURRENCYSYMBOL preferenceDialogFragmentCompat CURRENCYSYMBOL preferenceDialogFragmentCompat = CURRENCYSYMBOL preferenceDialogFragmentCompat.newInstance(preference.getKey());
展会preferenceDialogFragmentCompat(CURRENCYSYMBOL preferenceDialogFragmentCompat);
} //显示定制对话框preference。
////////////////////////////////////////////////// ////////////////////////////////////////////// ////////////////////////////////////////////////// //////////////////////////////////////////////
//当我们使用自定义对话框preference回调。
@覆盖
公共片段getCallbackFragment(){
返回此;
} @覆盖
在preferenceDisplayDialog公共布尔(preferenceFragmentCompat preferenceFragmentCompat,preference preference){
最终字符串键= preference.getKey(); 如果(_CURRENCY_SYMBOL_OPTIONS.equals(键)){
showCurrencySymbol preferenceDialogFragmentCompat(preference);
返回true;
} 返回false;
}
//当我们使用自定义对话框preference回调。
////////////////////////////////////////////////// //////////////////////////////////////////////
CURRENCYSYMBOL preferenceDialogFragmentCompat.java
公共类CURRENCYSYMBOL preferenceDialogFragmentCompat扩展$ P $ {pferenceDialogFragmentCompat 公共CURRENCYSYMBOL preferenceDialogFragmentCompat(){
} 公共静态CURRENCYSYMBOL preferenceDialogFragmentCompat的newInstance(字符串键){
CURRENCYSYMBOL preferenceDialogFragmentCompat片段=新CURRENCYSYMBOL preferenceDialogFragmentCompat();
束B =新包(1);
b.putString(密钥,密钥);
fragment.setArguments(二);
返回片段;
} @覆盖
公共无效onDialogClosed(布尔positiveResult){
如果(positiveResult){
...
this.get preference()setSummary(getSummary());
...
}
}
}
preferences.xml
< android.support.v7 preference.List preference
机器人:标题=@字符串/ preference_currency_symbol_title
机器人:键=_ CURRENCY_SYMBOL_OPTIONS/>
请注意,你需要在你的主题添加以下的项目。
<项目名称=preferenceTheme> @风格/ preferenceThemeOverlay< /项目>
有关主题一些错误
然而,主题ING还不完善 - <一个href=\"http://stackoverflow.com/questions/32070670/$p$pferencefragmentcompat-requires-$p$pferencetheme-to-be-set\">$p$pferenceFragmentCompat需要设置 preferenceTheme
这是一个已知的问题:
有提出解决办法但是,解决办法只能将就对于V14及以上,不V7及以上的工作。
I had gone through How to use AppCompatPreferenceActivity?
I'm using appcompat-v7:23.0.1
and support-v4:23.0.1
Before using AppCompayPreferenceActivity
, my PreferenceActivity
looks the following without toolbar.
Without Toolbar
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();
}
}
After using AppCompayPreferenceActivity
, with some Toolbar
initialization code, the outcome looks as the following
The outcome isn't as expected :-
- The
Toolbar
overlaps withPreferenceFragment
- When touching on
Toolbar
, the event will be consumed byPreferenceFragment
. This means, when you touch on toolbar, "Table header" preference will be touched instead.
With Toolbar
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);
}
}
My toolbar is
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>
I was wondering, what I had did wrong, which causes Toolbar
overlapped with PreferenceFragment
After some research and getting advice from Ian Lake, I manage to make it work by
- Change from
AppCompatPreferenceActivity
, toAppCompatActivity
+PreferenceFragmentCompat
The following solution is workable for API 10 and above.
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();
}
However, by doing so, there is a shortcoming. Your previous custom DialogPreference
no longer work. To solve this, you need to
- Replace
DialogPreference
withPreferenceDialogFragmentCompat
. - Replace
DialogPreference
in XML withListPreference
. (I'm not exactly sure whetherListPreference
as replacement is a right way. It works anyway) - Manually show
PreferenceDialogFragmentCompat
, inPreferenceFragmentCompat
'sonPreferenceDisplayDialog
.
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" />
Please note that, you need to add the following item in your theme.
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
Some bugs regarding theme
However, the theme-ing isn't perfect yet - PreferenceFragmentCompat requires preferenceTheme to be set
This is a known issues : https://code.google.com/p/android/issues/detail?id=183376
There is proposed workaround http://stackoverflow.com/a/32108439/72437 But, the workaround will only work for v14 and above, not v7 and above.
这篇关于通过应用程序兼容性preferenceActivity添加工具栏preferenceActivity导致重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!