在PreferenceAcitity中使用Fragement时避免额外的Left和RightPadding
On Android 4.4
遇到过这种问题:
![]()
注意到。上面的ActionBar部分的左右各有48像素的padding。
要了解该问题的成因,要首先了解其结构:
- 该页面的Activity是一个PreferenceActivity,是个三级页面,是通过上一个PreferenceActivity调用startWithFragment启动的;
- 该页面的内容是放置了一个Fragment。该Fragment。实现了自己的ActionBar (ActionBar是来自Fragment而不是Acitivity)
我们通过hierarchyviewer看一下它的界面构成:

截图中的PreferenceFrameLayout就是这个关键类。
PreferenceAcitivity的布局文件是 frameworks/base/core/res/res/layout/preference_list_fragment.xml
可是。PreferenceFrameLayout类的风格却是由android内部的一个风格控制的:(frameworks/base/core/res/res/values/styles.xml)
<stylename="Widget.Holo.PreferenceFrameLayout">
<itemname="android:borderTop">0dip</item>
<itemname="android:borderBottom">@dimen/preference_fragment_padding_bottom</item>
<itemname="android:borderLeft">?attr/preferenceFragmentPaddingSide</item>
<itemname="android:borderRight">?attr/preferenceFragmentPaddingSide</item>
</style>
preferenceFragmentPaddingSide的值在不同平台值不同。在这里定义为16dip (16*3 = 48)
他通过默认风格引用的(frameworks/base/core/res/res/values/themes.xml)
<itemname="preferenceFrameLayoutStyle">@android:style/Widget.PreferenceFrameLayout</item>
这样,在默认情况下,PreferenceActivity都会带有边框Padding。
可是PreferenceFrameLayout另一个特殊属性:layout_removeBorders。
这个属性为true时,表示忽略padding。
他相应的是PreferenceFrameLayout.LayoutParams.removeBorders 这个变量。
PreferenceFragment这个和PreferenceActivity匹配的Fragment,就具有该属性:(frameworks/base/core/res/res/layout/preference_list_fragment.xml)
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@android:color/transparent"
android:layout_removeBorders="true">
.....
因此。PreferenceFragment放到PreferenceActivity的时候。这个边框就没有了。
不幸的时,以上全部的layout属性都是Private的。无法更改,因此,当我们的Fragment不是PreferenceFragment或者它的派生类的时候,就不能消除边框了。
只是,我们还有其它的解决方法,万幸的是PreferenceFrameLayout和PreferenceFrameLayout.LayoutParams都是Public的。因此,我们能够直接代码改动:
@Override
public View onInflateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(
....., container,false);
if(container != null) {
ViewGroup prefView = (ViewGroup)container.getParent();
ViewGroup.LayoutParams layoutParams = (ViewGroup.LayoutParams)prefView.getLayoutParams();
if(layoutParams instanceofPreferenceFrameLayout.LayoutParams) {
((PreferenceFrameLayout.LayoutParams)layoutParams).removeBorders =true;
}
}
returnview;
}
在Fragment的onInflateView函数中强制改动removeBorders就能够达到这种目的。
当然,须要正确找到PreferenceFrameLayout的第一个子View,改动该子View的LayoutParams才干生效,这须要依据不同的情况来分别对待。
在PreferenceAcitity中使用Fragement时避免额外的Left和RightPadding的更多相关文章
- 在Amazon FreeRTOS V10中使用运行时统计信息
在MCU on Eclipse网站上看到Erich Styger在8月2日发的博文,一篇关于在Amazon FreeRTOS V10中使用运行时统计信息的文章,本人觉得很有启发,特将其翻译过来以备参考 ...
- 在php中定义常量时,const与define的区别?
问]在php中定义常量时,const与define的区别? [答]使用const使得代码简单易读,const本身就是一个语言结构,而define是一个函数.另外const在编译时要比define快很 ...
- AndRodi Strudio中的按钮时件
AndRodi Studio中的按钮时件注册一定要写在onCraete中 @Override protected void onCreate(Bundle savedInstanceState) { ...
- 在MySQL向表中插入中文时,出现:incorrect string value 错误
在MySQL向表中插入中文时,出现:incorrect string value 错误,是由于字符集不支持中文.解决办法是将字符集改为GBK,或UTF-8. 一.修改数据库的默认字符集 ...
- Android Tips: 在给drawable中添加图片资源时,文件名必须全小写
在给drawable中添加图片资源时,文件名必须全小写
- 在查询时将查询条件放入Session中,导出时直接根据qpniRGaFiler取查询条件即可
在查询时将查询条件放入Session中,导出时直接根据qpniRGaFiler取查询条件即可
- 使用ueditor中的setContent() 时经常报innerHtml错误(笔记)
1)今天遇到个问题,使用ueditor中的setContent() 时经常报innerHtml错误:网上找了下解决方案:发现这个可以用: 不能创建editor之后马上使用ueditor.setCont ...
- 在Linux下安装PHP过程中,编译时出现错误的解决办法
在Linux下安装PHP过程中,编译时出现configure: error: libjpeg.(a|so) not found 错误的解决办法 configure: error: libjpeg.(a ...
- Java中字符串比较时==和equals的区别
==是比较两个字符串引用的地址是否相同,即是否指向同一个对象,而equals方法则比较字符串的内容是否相同. 例如String a = "abc"; String b = &quo ...
随机推荐
- 杭电oj2012-2021
2012 素数判定 #include <stdio.h> #include <math.h> int main() { int x,y,i,j,a,flag; while(s ...
- OpenGL入门学习(三)
http://developer.178.com/201103/94954704639.html 在第二课中,我们学习了如何绘制几何图形,但大家如果多写几个程序,就会发现其实还是有些郁闷之处.例如:点 ...
- Scenes in Cocos2D
https://www.makeschool.com/docs/?source=mgwu#!/cocos2d/1.0/scenes Scenes in Cocos2D In Cocos2D you h ...
- 戴文的Linux内核专题:03 驱动程序【转】
转自:http://www.lai18.com/content/432194.html 驱动程序是使内核能够沟通和操作硬件或协议(规则和标准)的小程序.没有驱动程序,内核不知道如何与硬件沟通或者处理协 ...
- UVA 10801 Lift Hopping 最短路
2种方式直接代码就可以了.注意首次不需要60S的转换 #include <map> #include <set> #include <list> #include ...
- 应用程序已被Java安全阻止
提示 您的安全设置已阻止自签名的应用程序运行 控制面板-JAVA-安全-例外站点-https://域名或IP/或http://域名或IP/,注意结尾必须要加/否则还是会一直提示被阻止
- C#发送POST,GET,DELETE请求API,并接受返回值
发送POST请求 /// <summary> /// API发送POST请求 /// </summary> /// <param name="url" ...
- Tarjan缩点+Spfa最长路【p3627】[APIO2009] 抢掠计划
Description Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是,Siruseri ...
- linux下使用gcc/g++编译代码时gets函数有错误
今天在linux中使用个g++编译一个名为myfirst.cpp的代码的时候,出现如下错误 myfirst.cpp: In function ‘int main()’:myfirst.cpp:11:2 ...
- (转)NSString 类的使用
官网连接地址:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Strings/Articles/Creat ...