Android Launcher3 修改workspace页面行数和列数

一、涉及文件与介绍:

1、packages/apps/Launcher3/res/xml/device_profiles.xml  图标的横竖排数量、图标大小、各种尺寸和间距主要是由device_profiles.xml这个配置文件来定义的。

2、packages\apps\Launcher3\src\com\android\launcher3\InvariantDeviceProfile.java    或DeviceProfile.java  device_profiles.xml是由DeviceProfile.java来加载并计算得到布局所需要的各种size padding 等参数。

3、packages/apps/Launcher3/res/xml/default_workspace_6x3.xml 定义了系统默认(出厂设置)的桌面内容

Hotseat.java Hotseat ViewGroup的代码实现

二、根据设备屏幕分辨率,然后设置不同行列数,在布局中修改 numHotseatIcons的数量即可。

packages\apps\Launcher3\res\xml\device_profiles.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2016 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<profiles xmlns:launcher="http://schemas.android.com/apk/res-auto" >

    <grid-option
        launcher:name="3_by_3"
        launcher:numRows="3"
        launcher:numColumns="3"
        launcher:numFolderRows="2"
        launcher:numFolderColumns="3"
        launcher:numHotseatIcons="3"
        launcher:dbFile="launcher_3_by_3.db"
        launcher:defaultLayoutId="@xml/default_workspace_3x3" >

        <display-option
            launcher:name="Super Short Stubby"
            launcher:minWidthDps="255"
            launcher:minHeightDps="300"
            launcher:iconImageSize="48"
            launcher:iconTextSize="13.0"
            launcher:canBeDefault="true" />

        <display-option
            launcher:name="Shorter Stubby"
            launcher:minWidthDps="255"
            launcher:minHeightDps="400"
            launcher:iconImageSize="48"
            launcher:iconTextSize="13.0"
            launcher:canBeDefault="true" />

    </grid-option>

    <grid-option
        launcher:name="4_by_4"
        launcher:numRows="4"
        launcher:numColumns="4"
        launcher:numFolderRows="3"
        launcher:numFolderColumns="4"
        launcher:numHotseatIcons="4"
        launcher:dbFile="launcher_4_by_4.db"
        launcher:defaultLayoutId="@xml/default_workspace_4x4" >

        <display-option
            launcher:name="Short Stubby"
            launcher:minWidthDps="275"
            launcher:minHeightDps="420"
            launcher:iconImageSize="48"
            launcher:iconTextSize="13.0"
            launcher:canBeDefault="true" />

        <display-option
            launcher:name="Stubby"
            launcher:minWidthDps="255"
            launcher:minHeightDps="450"
            launcher:iconImageSize="48"
            launcher:iconTextSize="13.0"
            launcher:canBeDefault="true" />

        <display-option
            launcher:name="Nexus S"
            launcher:minWidthDps="296"
            launcher:minHeightDps="491.33"
            launcher:iconImageSize="48"
            launcher:iconTextSize="13.0"
            launcher:canBeDefault="true" />

        <display-option
            launcher:name="Nexus 4"
            launcher:minWidthDps="359"
            launcher:minHeightDps="567"
            launcher:iconImageSize="54"
            launcher:iconTextSize="13.0"
            launcher:canBeDefault="true" />

        <display-option
            launcher:name="Nexus 5"
            launcher:minWidthDps="335"
            launcher:minHeightDps="567"
            launcher:iconImageSize="54"
            launcher:iconTextSize="13.0"
            launcher:canBeDefault="true" />

    </grid-option>

    <grid-option
        launcher:name="5_by_5"
        launcher:numRows="5"
        launcher:numColumns="5"
        launcher:numFolderRows="4"
        launcher:numFolderColumns="4"
        launcher:numHotseatIcons="5"
        launcher:dbFile="launcher.db"
        launcher:defaultLayoutId="@xml/default_workspace_5x5" >

        <display-option
            launcher:name="Large Phone"
            launcher:minWidthDps="406"
            launcher:minHeightDps="694"
            launcher:iconImageSize="56"
            launcher:iconTextSize="14.4"
            launcher:canBeDefault="true" />

        <display-option
            launcher:name="Large Phone Split Display"
            launcher:minWidthDps="406"
            launcher:minHeightDps="694"
            launcher:iconImageSize="56"
            launcher:iconTextSize="14.4"
            launcher:canBeDefault="split_display" />

        <display-option
            launcher:name="Shorter Stubby"
            launcher:minWidthDps="255"
            launcher:minHeightDps="400"
            launcher:iconImageSize="48"
            launcher:iconTextSize="13.0"
            launcher:canBeDefault="true" />

    </grid-option>

</profiles>

三、其他调整详情介绍

step1: 需要根据预览图来定义好放在Workpace/AllApps/Hotseat里的图标数量。

操作提示: 重名6_by_3,代表6行3列,复制并改名一份default_workspace_6x3.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:defaultLayoutId="@xml/default_workspace_6x3"//默认配置文件
>

step2:定义图标占用的Cell的大小,每个图标占用的位置叫做Cell(图标focused后的阴影部分),以下只注释几个关键的属性

<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的位置

1: 在res/values/dimens.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-->

2:在src/com/android/launcher3/config/FeatureFlags.java中定义FeatueFlag以兼容原生的代码

public static final boolean FIXED_WORKSPACE_PADDING = true;//adding for defining workspace padding

3:DeviceProfile.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值,

需要更改如下:

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);
    }

修改文字与图标之间的间隙

res/values-sw600dp/dimens.xml:39:    <dimen name="dynamic_grid_icon_drawable_padding">2px</dimen>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值