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
我已经调查了所报告的重叠问题,因此无法重现该问题。
上面使用的完整代码产生以下内容:
如果我遗漏了一些东西,请通过此仓库让我知道,我将进行调查。
android support library v21,升级到支持库v21后,PreferenceActivity中没有ActionBar相关推荐
- 如何快速将Android Support Library项目升级到AndroidX
项目名右击 → Refactor → Migrate to AndroidX 有些项目会报错: you need to have compileSdk set to at least 28 in yo ...
- Android Support Library学习入门
1.Support Library介绍 官方文档上(原文:http://developer.android.com/tools/support-library/index.html)第一句话就指明了S ...
- Android Support Library 学习入门
0. 文前闲话 作为一个由原生桌面应用程序开发者(VC.Delphi)转行的Android菜鸟,虐心的事真是数不胜数:安装个开发工具下载个SDK需要整整一夜:早晨一上班点开Android Studio ...
- Android Support library
1, Android Support V4, V7, V13是什么?本质上就是三个java library.2, 为什么要有support库?如果在低版本Android平台上开发一个应用程序,而应用程 ...
- 如何更新android v7 support library,新手入门之Android Support Library | Soo Smart!
Support Library 这个支持包是一个一系列代码库,它提供了Android framework APIs的向后兼容的版本以及一些只有通过这个库包API才有的feature特性.每个Suppo ...
- 正确设置Android Support Library
2019独角兽企业重金招聘Python工程师标准>>> 为了支持低版本的Android,一般都要使用到Google提供的支持类库,但今天在设置支持类库的时候,总是遇到错误,弄了大半天 ...
- Android Support Library介绍
一.Android Support Library存在的原因 Android自第一代发布以来,它的版本更新迭代的速度可以说是非常快的,但是android又是一个移动操作系统,是面对所有的用户的,并不是 ...
- 【翻译】Android Support Library Features(二)
原文地址:http://developer.android.com/tools/support-library/features.html 在Android Support Library包中,包含了 ...
- Android Support Library v4、v7、v13
一.前言 关于Android Support Library,官网是这样解释的: "Android 支持库提供了诸多未内置于框架的功能.这些库提供向后兼容版本的新功能.框架中未包含的实用 U ...
最新文章
- 弱引用使用场景桌面_吃透Java基础十五:强引用、软引用、弱引用、虚引用
- pandas删除满足条件的行_入门Pandas练习
- 构造类斐波那契数列矩阵
- python3.7代码_Centos7源代码安装python3.7,centos7,源码,python37
- mplayer-php,mplayer+smplayer 前后端播放器安装
- Scala-Actor并行wordcount
- 机器学习 训练验证测试_测试前验证| 机器学习
- Day 4-7 -configparser模块
- JAVA——以ReentrantLock为例学习重入锁以及公平性问题
- matlab连续投影算法SPA使用示例
- 单点登录(java)
- 公交车查询系统软件测试,公交APP评测:谁是最好用的公交线路查询软件?
- php 测试网站打开速度,利用JS测试目标网站的打开响应速度_javascript技巧
- visio的替代者yEd Graph Editor
- python编程求长方形的面积_Python实现计算长方形面积(带参数函数demo)
- 离散数学_集合论部分_总结
- Python 微信公众号开发
- mysql常用日期的写法
- 阿里云服务器域名备案全过程详细讲解
- Visio日程规划图——论文计划进度图
热门文章
- 删除右键菜单-美图看看美图秀秀的选项
- 【Qt编程】基于Qt的词典开发系列十二调用讲述人
- php可以转化为mp3吗,视频可以转换成mp3音频吗?
- office2010连接服务器响应慢,从网络位置打开文件时,Office 运行缓慢或停止响应 - Office | Microsoft Docs...
- POJ2420——A Star not a Tree?
- Excel AI - 推出 AI 智能函数,太过强大
- FPGA udp纯逻辑编写,Xilinx ise14.7平台,工程验证千兆无丢包 加200k128通道采集
- 2.面经-中移互联网
- 视频云技术系列 - 5G 700MHz关键技术,大塔小塔模式和无线上行增强技术原理
- 为什么我不建议你用 if-else ?