2019-04-17 Android 设置界面 Preferen
PreferenceFragmentCompat 除了UI以外的用法,笔记在此:https://www.jianshu.com/p/9b7ea3450f0c
不居中问题:
根据参考资料,
需要在styles.xml中,指定theme:

然而无论我加不加这句话
(<item name="preferenceTheme"> @style/PreferenceThemeOverlay.v14.Material</item>
)
,我的界面都是这样的:(其中标题栏和底下的bottombar写在外层的activity里)

如图所示,除了黑灰外,preference category 和 switch preference 的颜色是colorAccent
没有仔细研究过ui的我感觉这好像已经符合material design了,因为单击的时候有漂亮的水波纹动画效果。
唯一不满的是不知道为什么不居中,而是整体偏右
今天终于知道了答案
在style.xml中,Ctrl+鼠标左键点击刚才设置的主题,进入代码

长这样:

里面没有内容。
考虑到这一条style加不加效果相同,可能它其实和默认的一样;
看到上面的没有版本号的里面各项item的style最后都有Material一词,估计其实用的就是这个PreferenceThemOverlay 了
于是我们再随便ctrl + 鼠标左键点进去一个:

结果长这样:

再点进这个layout呢:

发现有一个iconFrame!!会不会就是他占着左边开头的位置呢!
我们进入设计视图:

至此,此谜已解:
preference fragment里面的条目不居中的原因是,左边留出了icon的空位,而我们没有设置icon的图标,所以这个空位是空白,看上去就整体偏右了
那么怎么办呢?
现在先让我们给每一条preference随意指定一张图片为icon试试效果:

效果如图:

而我们icon大小应该是48*48dp,让我们选择一些图片来试试
加了一些图标感觉不好看,视图用isIconSpaceReserved 置位false,无效
如下stackoverflow链接告诉我们,这是故意的,因为material design 要求空出这个icon的位置
https://stackoverflow.com/questions/5765186/how-to-add-icons-to-preference
material design 要求如下:
https://material.io/design/platform-guidance/android-settings.html#placement
这里提供了一个赏心悦目的小例子:

真是赏心悦目啊。
那么为什么我们做的图标就这么丑呢?因为太大了。那我们多padding一点试试,把padding加大到50%
同时根据例子,icon的颜色是深灰色,比一级字浅,比二级字深
加到50% 美如画