需求:Launcher 最基本的修改就是Workspace Hotseat AllApps的布局及出厂默认设置
修改原理:
res/xml/device_profiles.xml 图标的横竖排数量、图标大小、各种尺寸和间距主要是由device_profiles.xml这个配置文件来定义的。
DeviceProfile.java device_profiles.xml是由DeviceProfile.java来加载并计算得到布局所需要的各种size padding 等参数。
res/xml/default_workspace_6x3.xml 定义了系统默认(出厂设置)的桌面内容
Hotseat.java Hotseat ViewGroup的代码实现
修改步骤:
step1: 需要根据预览图来定义好放在Workpace/AllApps/Hotseat里的图标数量。
操作提示: 重名6_by_3,代表6行3列,复制并改名一份default_workspace_6x3.xml
XML
<grid-option
launcher:name="6_by_3"
launcher:numRows="3" //行数
launcher:numColumns="6" //列数
launcher:numSearchContainerColumns="3"
launcher:numFolderRows="3" //文件夹行数
launcher:numFolderColumns="3"//文件夹列数
launcher:numHotseatIcons="5"//热座图标数量
launcher:hotseatColumnSpanLandscape="1"
launcher:numAllAppsColumns="6"//AllApps视图的列数
launcher:isScalable="true"
launcher:devicePaddingId="@xml/paddings_6x5"
launcher:dbFile="launcher_6_by_5.db"
launcher:defaultLayoutId="@xml/default_workspace_6x3"//默认配置文件
launcher:deviceCategory="tablet" >
step2:定义图标占用的Cell的大小,每个图标占用的位置叫做Cell(图标focused后的阴影部分),以下只注释几个关键的属性
XML
<display-option
launcher:name="Tablet"
launcher:minWidthDps="900"
launcher:minHeightDps="820"
launcher:minCellHeight="120" //此处标注的是最小Cell的高度,为什么不是标注实际高度呢?
launcher:minCellWidth="102"//因为Cell是由Workspace的占用区域,根据图标数量和borderSpace边距大小算出的!
launcher:minCellHeightLandscape="104"//横屏布局用这个,其他同理
launcher:minCellWidthLandscape="120"
launcher:iconImageSize="64"//图标大小
launcher:iconTextSize="14"//文本大小
launcher:borderSpaceHorizontal="16"//水平方向Cell的边距
launcher:borderSpaceVertical="64"//垂直方向Cell的边距
launcher:borderSpaceLandscapeHorizontal="16"
launcher:borderSpaceLandscapeVertical="16"
launcher:horizontalMargin="54"
launcher:horizontalMarginLandscape="120"
launcher:allAppsCellWidth="96"
launcher:allAppsCellHeight="142"
launcher:allAppsCellWidthLandscape="126"
launcher:allAppsCellHeightLandscape="126"
launcher:allAppsIconSize="60"
launcher:allAppsIconTextSize="14"
launcher:allAppsBorderSpaceHorizontal="8"
launcher:allAppsBorderSpaceVertical="16"
launcher:allAppsBorderSpaceLandscape="16"
launcher:hotseatBorderSpace="29" //热座图标之间的边距
launcher:hotseatBorderSpaceLandscape="14"//横屏热座图标之间的边距
launcher:canBeDefault="true" />
修改Workspace的位置
step1: 在res/values/dimens.xml添加
XML
<!--Kevin.Ye added start-->
<dimen name="workspace_padding_side">4dp</dimen>
<dimen name="workspace_padding_top">4dp</dimen>
<dimen name="workspace_padding_bottom">4dp</dimen>
<!--Kevin.Ye added end-->
step2:
在src/com/android/launcher3/config/FeatureFlags.java中定义FeatueFlag以兼容原生的代码
java
public static final boolean FIXED_WORKSPACE_PADDING = true;//adding for defining workspace padding
step3:DeviceProfile.java中添加定义
java
import com.android.launcher3.config.FeatureFlags;
/*Kevin.Ye added start*/
private int workspacePaddingTopPx = 50;
private int workspacePaddingBottomPx = 50;
private int workspacePaddingSidePx = 50;
/*Kevin.Ye added end*/
在构造函数的最后读取value
//Kevin.Ye added start
if(FeatureFlags.FIXED_WORKSPACE_PADDING) {
workspacePaddingTopPx = res.getDimensionPixelSize(R.dimen.workspace_padding_top);
workspacePaddingBottomPx = res.getDimensionPixelSize(R.dimen.workspace_padding_bottom);
workspacePaddingSidePx = res.getDimensionPixelSize(R.dimen.workspace_padding_side);
}
//added end
在updateWorkspacePadding函数中替换原来通过计算的方式得到的padding
private void updateWorkspacePadding() {
Rect padding = workspacePadding;
/*Kevin.Ye added for workspace position*/
if(FeatureFlags.FIXED_WORKSPACE_PADDING) {
if(isVerticalBarLayout()) {
}else{
padding.left = workspacePaddingSidePx;
padding.right = workspacePaddingSidePx;
padding.top = workspacePaddingTopPx;
padding.bottom = workspacePaddingBottomPx;
}
insetPadding(workspacePadding, cellLayoutPaddingPx);
return;
}
通过调试发现,DeviceProfile并不是单例,因此需要把Resource传进来,每次调用都要重新获取resource里定义的padding值,
需要更改如下:
java
private void updateWorkspacePadding(Resources res) {
Rect padding = workspacePadding;
if (isVerticalBarLayout()) {
padding.top = 0;
padding.bottom = edgeMarginPx;
if (isSeascape()) {
padding.left = hotseatBarSizePx;
padding.right = hotseatBarSidePaddingStartPx;
} else {
padding.left = hotseatBarSidePaddingStartPx;
padding.right = hotseatBarSizePx;
}
} else {
// Pad the bottom of the workspace with search/hotseat bar sizes
if(FeatureFlags.STATIC_WORKSPACE_PADDING){//Kevin.Ye modified
int workspacePaddingTopPx = res.getDimensionPixelSize(R.dimen.workspace_padding_top);
int workspacePaddingBottomPx = res.getDimensionPixelSize(R.dimen.workspace_padding_bottom);
int workspacePaddingSidePx = res.getDimensionPixelSize(R.dimen.workspace_padding_side);
Log.d("profile","workspace padding top:"+workspacePaddingTopPx+" bottom:"+workspacePaddingBottomPx+" side:"+workspacePaddingSidePx);
padding.set(workspacePaddingSidePx,workspacePaddingTopPx,workspacePaddingSidePx,workspacePaddingBottomPx);
}else{
int hotseatTop = hotseatBarSizePx;
int paddingBottom = hotseatTop + workspacePageIndicatorHeight
+ workspaceBottomPadding - mWorkspacePageIndicatorOverlapWorkspace;
int paddingTop = workspaceTopPadding + (isScalableGrid ? 0 : edgeMarginPx);
int paddingSide = desiredWorkspaceHorizontalMarginPx;
padding.set(paddingSide, paddingTop, paddingSide, paddingBottom);
}
}
insetPadding(workspacePadding, cellLayoutPaddingPx);
}
修改文字与图标之间的间隙
XML
res/values-sw600dp/dimens.xml:39: <dimen name="dynamic_grid_icon_drawable_padding">2px</dimen>