Launcher3控制桌面(WorkSpace)应用图标的行列数其实核心来自于InvariantDeviceProfile
类,这个类初始化了Launcher3的许多参数。
根据它的成员可以看到它至少控制了Launcher3的如下参数
/** * Number of icons per row and column in the workspace. 在WorkSpace中行列图标数量 */
public int numRows;
public int numColumns;
/** * Number of icons per row and column in the folder. 文件夹中图标行列数 */
public int numFolderRows;
public int numFolderColumns;
public float iconSize;
public float landscapeIconSize;
public int iconBitmapSize;
public int fillResIconDpi;
public float iconTextSize;
/** * Number of icons inside the hotseat area. Hotseat图标数量 */
public int numHotseatIcons;
其流程大体如下:
可以看到,默认的行列数初始化是在InvariantDeviceProfile
类中处理的,而且其默认参数是通过计算设备宽高,并且与device_profiles.xml
文件中取宽高最接近的一项来应用。
修改Workspace应用的行列数很容易,从上面整个通路里有多个地方都可以做到,但这里为了统一,还是直接修改device_profiles.xml
预定义的选项即可。
要了解当前设备匹配的是哪一项,可以通过打Log获取:
// File: src\com\android\launcher3\InvariantDeviceProfile.java
public InvariantDeviceProfile(Context context){
/....
InvariantDeviceProfile closestProfile = closestProfiles.get(0);
Log.d("GCAT DBG", "InvariantDeviceProfile: " + closestProfile.name);
numRows = closestProfile.numRows;
//.....
}
然后修改xml文件中的对应的Profile的参数即完成修改,当然你也可以将Profile中的参数删的只剩下一个,这样不管是什么分辨率和DPI都只会应用一个,也可以按照实际需求适配不同的DPI和屏幕分辨率:
// File: res\xml\device_profiles.xml
<?xml version="1.0" encoding="utf-8"?>
<profile
launcher:name="Nexus 10_mod"
launcher:minWidthDps="727"
launcher:minHeightDps="1207"
launcher:numRows="5" //行数
launcher:numColumns="7" //列数
launcher:numFolderRows="4" //文件夹内行数
launcher:numFolderColumns="5" //文件夹内列数
launcher:iconSize="76" //Icon大小
launcher:iconTextSize="14.4" //提示文本大小
launcher:numHotseatIcons="7" //Hotseat图标数量
launcher:defaultLayoutId="@xml/default_workspace_5x6" //默认workspace布局文件,此处可同时修改默认存在hotseat或桌面的应用
/>