android support library v21,升级到支持库v21后,PreferenceActivity中没有ActionBar

请在以下位置找到GitHub Repo:

与您自己的代码非常相似,但添加了xml以允许设置标题:

继续使用PreferenceActivity:

settings_toolbar.xml :

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

android:id="@+id/toolbar"

app: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"

app:navigationIcon="?attr/homeAsUpIndicator"

app:title="@string/action_settings"

/>

SettingsActivity.java :

public class SettingsActivity extends PreferenceActivity {

@Override

protected void onPostCreate(Bundle savedInstanceState) {

super.onPostCreate(savedInstanceState);

LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();

Toolbar bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);

root.addView(bar, 0); // insert at top

bar.setNavigationOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

finish();

}

});

}

}

Result :

UPDATE(姜饼兼容性):

正如指出的在这里,姜饼设备是在这条线返回NullPointerException异常:

LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();

固定:

SettingsActivity.java :

public class SettingsActivity extends PreferenceActivity {

@Override

protected void onPostCreate(Bundle savedInstanceState) {

super.onPostCreate(savedInstanceState);

Toolbar bar;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

LinearLayout root = (LinearLayout) findViewById(android.R.id.list).getParent().getParent().getParent();

bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);

root.addView(bar, 0); // insert at top

} else {

ViewGroup root = (ViewGroup) findViewById(android.R.id.content);

ListView content = (ListView) root.getChildAt(0);

root.removeAllViews();

bar = (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 = bar.getHeight();

}

content.setPadding(0, height, 0, 0);

root.addView(content);

root.addView(bar);

}

bar.setNavigationOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

finish();

}

});

}

}

以上任何问题都请通知我!

更新2:着色解决方案

正如许多开发者笔记中指出的PreferenceActivity那样,不支持元素的着色,但是通过利用一些内部类,您可以实现此目的。直到删除这些类。(使用appCompat support-v7 v21.0.3可以工作)。

添加以下导入:

import android.support.v7.internal.widget.TintCheckBox;

import android.support.v7.internal.widget.TintCheckedTextView;

import android.support.v7.internal.widget.TintEditText;

import android.support.v7.internal.widget.TintRadioButton;

import android.support.v7.internal.widget.TintSpinner;

然后重写该onCreateView方法:

@Override

public View onCreateView(String name, Context context, AttributeSet attrs) {

// Allow super to try and create a view first

final View result = super.onCreateView(name, context, attrs);

if (result != null) {

return result;

}

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {

// If we're running pre-L, we need to 'inject' our tint aware Views in place of the

// standard framework versions

switch (name) {

case "EditText":

return new TintEditText(this, attrs);

case "Spinner":

return new TintSpinner(this, attrs);

case "CheckBox":

return new TintCheckBox(this, attrs);

case "RadioButton":

return new TintRadioButton(this, attrs);

case "CheckedTextView":

return new TintCheckedTextView(this, attrs);

}

}

return null;

}

Result:

例子2

AppCompat 22.1

AppCompat 22.1引入了新的着色元素,这意味着不再需要使用内部类来实现与上次更新相同的效果。而是遵循以下步骤(仍然是onCreateView):

@Override

public View onCreateView(String name, Context context, AttributeSet attrs) {

// Allow super to try and create a view first

final View result = super.onCreateView(name, context, attrs);

if (result != null) {

return result;

}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

// If we're running pre-L, we need to 'inject' our tint aware Views in place of the

// standard framework versions

switch (name) {

case "EditText":

return new AppCompatEditText(this, attrs);

case "Spinner":

return new AppCompatSpinner(this, attrs);

case "CheckBox":

return new AppCompatCheckBox(this, attrs);

case "RadioButton":

return new AppCompatRadioButton(this, attrs);

case "CheckedTextView":

return new AppCompatCheckedTextView(this, attrs);

}

}

return null;

}

嵌套偏好屏幕

很多人都遇到了在嵌套s中包含工具栏的问题,但是,我找到了解决方案!-经过大量的反复试验!

将以下内容添加到您的SettingsActivity:

@SuppressWarnings("deprecation")

@Override

public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {

super.onPreferenceTreeClick(preferenceScreen, preference);

// If the user has clicked on a preference screen, set up the screen

if (preference instanceof PreferenceScreen) {

setUpNestedScreen((PreferenceScreen) preference);

}

return false;

}

public void setUpNestedScreen(PreferenceScreen preferenceScreen) {

final Dialog dialog = preferenceScreen.getDialog();

Toolbar bar;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {

LinearLayout root = (LinearLayout) dialog.findViewById(android.R.id.list).getParent();

bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);

root.addView(bar, 0); // insert at top

} else {

ViewGroup root = (ViewGroup) dialog.findViewById(android.R.id.content);

ListView content = (ListView) root.getChildAt(0);

root.removeAllViews();

bar = (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 = bar.getHeight();

}

content.setPadding(0, height, 0, 0);

root.addView(content);

root.addView(bar);

}

bar.setTitle(preferenceScreen.getTitle());

bar.setNavigationOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

dialog.dismiss();

}

});

}

之所以PreferenceScreen如此痛苦,是因为它们基于包装对话框,因此我们需要捕获对话框布局以向其添加工具栏。

工具栏阴影

通过设计导入,Toolbar不允许在v21之前的设备中进行加高和阴影处理,因此,如果您希望在其上加高,则Toolbar需要将其包装在中AppBarLayout:

`settings_toolbar.xml:

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="wrap_content">

.../>

别忘了将设计支持库添加为build.gradle文件中的依赖项:

compile 'com.android.support:support-v4:22.2.0'

compile 'com.android.support:appcompat-v7:22.2.0'

compile 'com.android.support:design:22.2.0'

Android 6.0

我已经调查了所报告的重叠问题,因此无法重现该问题。

上面使用的完整代码产生以下内容:

如果我遗漏了一些东西,请通过此仓库让我知道,我将进行调查。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值