今天遇到一个问题,需要修改Preference的宽度,让其与屏幕宽度一致。搞了一上午。
终于发现Preference的这个尺寸是在PreferenceFrameLayout中设置的。通过下面这段代码,
mBorderTop = a.getDimensionPixelSize(
com.android.internal.R.styleable.PreferenceFrameLayout_borderTop,
defaultBorderTop);
mBorderBottom = a.getDimensionPixelSize(
com.android.internal.R.styleable.PreferenceFrameLayout_borderBottom,
defaultBottomPadding);
mBorderLeft = a.getDimensionPixelSize(
com.android.internal.R.styleable.PreferenceFrameLayout_borderLeft,
defaultLeftPadding);
mBorderRight = a.getDimensionPixelSize(
com.android.internal.R.styleable.PreferenceFrameLayout_borderRight,
defaultRightPadding);
我们可以发现,可以在主题中对其进行定制。
在系统的主题中,我们可以看到如下语句
<!-- PreferenceFrameLayout attributes -->
<item name="preferenceFrameLayoutStyle">@android:style/Widget.Holo.PreferenceFrameLayout</item>
我们在看看这个定义
<style name="Widget.Holo.PreferenceFrameLayout">
<item name="android:borderTop">0dip</item>
<item name="android:borderBottom">@dimen/preference_fragment_padding_bottom</item>
<item name="android:borderLeft">@dimen/preference_fragment_padding_side</item>
<item name="android:borderRight">@dimen/preference_fragment_padding_side</item>
</style>
其中左右边界都不是0,所以,我们看到在3.0之后的版本的Preference的左右两边都有一定的距离,二以前的版本这两个值是0.其定义如下:
<style name="Widget.PreferenceFrameLayout">
<item name="android:borderTop">0dip</item>
<item name="android:borderBottom">0dip</item>
<item name="android:borderLeft">0dip</item>
<item name="android:borderRight">0dip</item>
</style>
在我们自动的主题中,我们可以自定义一个这个的主题。本来以为这样可以完事大吉了。结果发现这几个值不是Public的。应用不能自定义。
坏了。
最后,发现在PreferenceActivity和PreferenceFragment中,创建View的时候,用的是布局文件preference_list_content和preference_list_fragment.xml
决定打开看看。发现原来这个边界还可以在布局文件中控制。我这里用的是PreferenceFragment.Activity的应该类似。不过我并没有做实验。
有两种改法。方法一,在Fragment中的onCreateView的时候,用我们自己的的Layout文件替代系统的Layout文件。只是注意文件的Id和布局要和系统的一样。
只不过把文件中设置ListView的Padding的地方改掉就可以了。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.my_preference_list_fragment, container,
false);
}
方法二:任然使用系统提供的Layout,只不过在onCreateView中用代码设置Padding,代码如下。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View parent = super.onCreateView(inflater, container, savedInstanceState);
if(null != parent)
{
View listView = parent.findViewById(android.R.id.list);
if(null != listView)
{
listView.setPadding( 0, listView.getPaddingTop(), 0, listView.getPaddingBottom());
}
}
return parent;
}
顺便提一下,在实验过程中,发现,如果要修改PreferanceCaegory的Layout定义是在preference_category_holo.xml中。
我们完全可以做一个类似的布局文件。然后再Preference的XML中,用android:layout指定我们自己的布局文件。
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/listSeparatorTextViewStyle"
android:id="@+android:id/title"
android:paddingLeft="@dimen/preference_item_padding_side"
android:paddingRight="@dimen/preference_item_padding_side" />
我们可以修改这个Padding,和样式,来控制Category的外观和位置。
- 网页或微信小程序中使元素占满整个屏幕高度
在项目中经常要用到一个容器元素占满屏幕高度和宽度,然后再在这个容器元素里放置其他元素. 宽度很简单就是width:100% 但是高度呢,我们知道的是height:100%必须是在父元素的高度给定了的情 ...
- Android ScrollView 子控件不占满的问题
经常碰到很笨的 ScrollView的子控件无法占满 ScrollView 的空间的问题. 其实只需要加一行,android:fillViewport="true" 但不加上这行就 ...
- Android中的Preference结构的设计与实现
本文主要通过分析源代码来分享Preference的设计和实现方式,让开发者们在今后更加顺手地使用和扩展Preference类,或者在设计其他类似的界面和功能时可以提供参考帮助. Preference概 ...
- Android中自定义Preference
一.需求 开发横屏设备的app时,发现preference显示的都是上下结构,因此需要自定义preference实现横屏显示. 二.layout实现 <?xml version="1. ...
- Android中调用Paint的measureText()方法取得字符串显示的宽度值
1 public static float GetTextWidth(String text, float Size) { //第一个参数是要计算的字符串,第二个参数是字提大小 2 T ...
- 自定义Dialog宽度占满屏幕
一.自定义Dialog继承Dialog public class MyDialog extends Dialog { 二.为Dialog设置样式 在style中建立新样式继承 @android:sty ...
- 16、css实现div中图片占满整个屏幕
<div class="img"></div> .img{ background: url("../assets/image/img.png&qu ...
- iframe框架默认占满整个屏幕
<script language="JavaScript"> if (window != top) { top.location.href = location.hre ...
- Android中dip、dp、sp、pt和px的区别
1.概述 过去,程序员通常以像素为单位设计计算机用户界面.例如:图片大小为80×32像素.这样处理的问题在于,如果在一个每英寸点数(dpi)更高的新显示器上运行该程序,则用户界面会显得很小.在有些情况 ...
随机推荐
- pthread_detach pthread_join pthread_create
pthread_create:创建线程以后线程直接开始运行: pthread_detach pthread_join:线程资源的释放方式. 创建一个线程默认的状态是joinable, 如果一个线程结束 ...
- UVa OJ 140 - Bandwidth (带宽)
Time limit: 3.000 seconds限时3.000秒 Problem问题 Given a graph (V,E) where V is a set of nodes and E is a ...
- sencha touch pull-refresh-panel 面板下拉刷新
转自:http://www.cnblogs.com/mlzs/archive/2013/06/04/3117518.html 此效果手机未测试,目测没问题,我是搬运工... 演示地址:http://s ...
- Js日期选择器并自动加入到输入框中
<html> <head> <title>Js日期选择器并自动加入到输入框中</title> <meta http-equiv="con ...
- BlockingQueue详解
本例介绍一个特殊的队列:BlockingQueue,如果BlockingQueue是空的,从BlockingQueue取东西的操作将会被阻断进入 等待状态,直到BlockingQueue进了东西才会被 ...
- Component creation must be done on Event Dispatch Thread错误解决方法
在用java swing 做例子,给页面设置皮肤样式的时候出现了这个错误: org.jvnet.substance.api.UiThreadingViolationException: Compone ...
- iOS-使用Xcode拉伸图片
如果要制作一个类似于QQ消息气泡的图片,该如何制作呢?android中可以使用.9图片指定图片中的某一部分拉伸,那iOS中类似的功能要如何实现呢,Xcode提供了类似的功能.具体步骤如下: 1.选择需 ...
- C开发基础--函数调用栈
发现有一些问题几乎是所有的新人都会遇到,而且也常因为缺乏一些基本的知识而无从下手.函数调用栈的内容就是其中之一.于是花点时间把以前写的内容整理出来. 程序在运行期间,内存中有一块区域,用来实现程序的函 ...
- Node.js 创建HTTP服务器(经过测试,这篇文章是靠谱的T_T)
Node.js 创建HTTP服务器 如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi. 从这个角度看,整个& ...
- editplus快捷键大全之editplus光标快捷键
前面我们讲了editplus快捷键大全之editplus文件快捷键,现在我们介绍一下editplus快捷键大全之editplus光标快捷键 移动光标到上一个制表符Shift+Tab 移动光标到上一个制 ...