Android Launcher3修改行数,列数,布局等;隐藏HostSeat,使用户无法拖拽应用图标到Hotseat

时间:2023-01-24 14:24:54

修改Launcher的行列数比较简单,在DeviceProfile方法中的构造方法中,可以修改行数,列数等。但是更严格的话需要到DynamicGrid方法中,调用DeviceProfile该方法时根据屏幕大小,动态控制行列数。图标大小等也可以在这里更改。

如果需要实现隐藏hostseat,就把hostseat的图标设置为0,但是会有一个地方报错,到时候可以做一下错误处理就可以了。当hostseat的数量为0时,返回屏幕的宽就可以了。这里不再贴代码展示。


    DeviceProfile(String n, float w, float h, float r, float c,
float is, float its, float hs, float his, int dlId) {
// Ensure that we have an odd number of hotseat items (since we need to place all apps)
if (!LauncherAppState.isDisableAllApps() && hs % 2 == 0) {
throw new RuntimeException("All Device Profiles must have an odd number of hotseat spaces");
}

name = n;
minWidthDps = w;
minHeightDps = h;
numRows = r;
//修改行数
numRows=3;
numColumns = c;
//修改列数
numColumns=6;

iconSize = is;
//修改图标大小
iconSize=90;
iconTextSize = its;
//修改应用名字大小
// iconTextSize=10;
numHotseatIcons = hs;
//修改hostseat图标数量
numHotseatIcons=0;

hotseatIconSize = his;
defaultLayoutId = dlId;
}

控制屏幕的布局则是通过layout方法去设置,由于代码繁多,这里选择性去贴代码讲解。

把hostseat的图标总数设为0后,用户就无法拖拽图标到这里了。要隐藏他的布局,可以通过设置他的高度,也可以通过其他布局,把它挣到屏幕外。如下是设置hostseat布局的代码

// Layout the hotseat
View hotseat = launcher.findViewById(R.id.hotseat);
lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
if (hasVerticalBarLayout) {
// Vertical hotseat
lp.gravity = Gravity.END;
lp.width = hotseatBarHeightPx;
lp.height = LayoutParams.MATCH_PARENT;
hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
} else if (isTablet()) {
// Pad the hotseat with the workspace padding calculated above
lp.gravity = Gravity.BOTTOM;
lp.width = LayoutParams.MATCH_PARENT;
lp.height = hotseatBarHeightPx;
hotseat.setPadding(edgeMarginPx + padding.left, 0,
edgeMarginPx + padding.right,
2 * edgeMarginPx);
} else {
// For phones, layout the hotseat without any bottom margin
// to ensure that we have space for the folders
lp.gravity = Gravity.BOTTOM;
lp.width = LayoutParams.MATCH_PARENT;
lp.height = hotseatBarHeightPx;
hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0,
2 * edgeMarginPx, 0);
}
hotseat.setLayoutParams(lp);


其他搜索栏,workspace,indicators,AllApps,Overview等的大小设置和launcher布局的控制都是在这里设置的

这里就相当于xml中的layout一样,控制屏幕的布局,只是换了一种写法。