需求:长按workspace空白处,Home Settings添加launcher界面布局3x3,4x4,5x5设置

添加布局文件,当网格设置关闭时,所有设置项置灰不允许点击

  • 源码:/vendor/mediatek/proprietary/packages/apps/Launcher3/res/xml/launcher_preferences.xml
 // add for grid options settings<PreferenceCategory android:key="grid_options_category"android:order="100"><SwitchPreferenceandroid:key="pref_grid_options"android:title="@string/grid_options"android:defaultValue="false"android:persistent="true" /><CheckBoxPreferenceandroid:dependency="pref_grid_options"android:persistent="true"android:key="3_by_3"android:title="3x3" />  <CheckBoxPreferenceandroid:dependency="pref_grid_options"android:persistent="true"android:key="4_by_4"android:title="4x4" /> <CheckBoxPreferenceandroid:dependency="pref_grid_options"android:persistent="true"android:key="5_by_5"android:title="5x5" /> </PreferenceCategory>     // end

使能GridOptionsProvider

  • 源码:/vendor/mediatek/proprietary/packages/apps/Launcher3/AndroidManifest-common.xml
<!--The content provider for exposing various launcher grid options.TODO: Add proper permissions--><providerandroid:name="com.android.launcher3.graphics.GridOptionsProvider"android:authorities="${packageName}.grid_control"android:exported="true"android:enabled="true" />

Launcher布局切换设置功能

  • 源码:/vendor/mediatek/proprietary/packages/apps/Launcher3/src/com/android/launcher3/settings/SettingsActivity.java
import com.android.launcher3.util.SecureSettingsObserver;// add for grid options settings
import com.android.launcher3.InvariantDeviceProfile;
import androidx.preference.CheckBoxPreference;
import android.content.Context;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
// endpublic class SettingsActivity extends FragmentActivityimplements OnPreferenceStartFragmentCallback, OnPreferenceStartScreenCallback,SharedPreferences.OnSharedPreferenceChangeListener{..../*** This fragment shows the launcher preferences.*/public static class LauncherSettingsFragment extends PreferenceFragmentCompat implementsPreference.OnPreferenceChangeListener{// add for grid options settings  private InvariantDeviceProfile mInvariantDeviceProfile;private static final String KEY_GRIDNAME_3X3 = "3_by_3";private static final String KEY_GRIDNAME_4X4 = "4_by_4";private static final String KEY_GRIDNAME_5X5 = "5_by_5";     private CheckBoxPreference checkbox3by3;private CheckBoxPreference checkbox4by4;private CheckBoxPreference checkbox5by5;// end@Overridepublic void onCreatePreferences(Bundle savedInstanceState, String rootKey) {final Bundle args = getArguments();...// add for grid options settingsinitPreference();// end}@Overridepublic void onResume() {super.onResume();if (isAdded() && !mPreferenceHighlighted) {PreferenceHighlighter highlighter = createHighlighter();if (highlighter != null) {getView().postDelayed(highlighter, DELAY_HIGHLIGHT_DURATION_MILLIS);mPreferenceHighlighted = true;} else {requestAccessibilityFocus(getListView());}}// add for grid options settings mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(getContext());updatePreference();// end}// add for grid options settings private void initPreference(){checkbox3by3 = (CheckBoxPreference)findPreference(KEY_GRIDNAME_3X3);checkbox3by3.setOnPreferenceChangeListener(this);checkbox4by4 = (CheckBoxPreference)findPreference(KEY_GRIDNAME_4X4);checkbox4by4.setOnPreferenceChangeListener(this);checkbox5by5 = (CheckBoxPreference)findPreference(KEY_GRIDNAME_5X5);checkbox5by5.setOnPreferenceChangeListener(this);updatePreference();}private void updatePreference(){String mGrigdName = mInvariantDeviceProfile.getCurrentGridName(getContext());checkbox3by3.setChecked((mGrigdName.equals(KEY_GRIDNAME_3X3)) ? true : false);checkbox4by4.setChecked((mGrigdName.equals(KEY_GRIDNAME_4X4)) ? true : false);checkbox5by5.setChecked((mGrigdName.equals(KEY_GRIDNAME_5X5)) ? true : false);}@Overridepublic boolean onPreferenceChange(Preference preference, Object newValue) {String key = preference.getKey();applyGrid(getContext(), key);updatePreference();return true;}    public int applyGrid(Context context, String name) {ContentValues values = new ContentValues();values.put("name", name);return context.getContentResolver().update(getUri(), values,null, null);}public Uri getUri() {return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority("com.android.launcher3.grid_control").appendPath("default_grid").build();}// end

All APP中布局更新

  • 源码:vendor/mediatek/proprietary/packages/apps/Launcher3/src/com/android/launcher3/allapps/AlphabeticalAppsList.java
 private void refreshRecyclerView() {if (mAdapter != null) {// add for the layout of all apps is updated synchronously after the grid option is changedmAdapter.setAppsPerRow(mLauncher.getDeviceProfile().inv.numAllAppsColumns);//mAdapter.notifyDataSetChanged();}}

设置3x3布局,前一屏没占满就创建下一屏

  • vendor/mediatek/proprietary/packages/apps/Launcher3/src/com/android/launcher3/model/LoaderCursor.java
protected boolean checkItemPlacement(ItemInfo item) {....//去掉以下代码/* if (!occupied.containsKey(item.screenId)) {GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);if (item.screenId == Workspace.FIRST_SCREEN_ID) {// Mark the first row as occupied (if the feature is enabled)// in order to account for the QSB.screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);}occupied.put(item.screenId, screen);}*/final GridOccupancy occupancy = occupied.get(item.screenId);
}

添加以上代码,launcher布局切换的功能基本实现

点击3x3,4x4等设置项后,会走GridOptionsProvider的update方法

  • 源码:/vendor/mediatek/proprietary/packages/apps/Launcher3/src/com/android/launcher3/graphics/GridOptionsProvider.java
@Overridepublic int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {if (!KEY_DEFAULT_GRID.equals(uri.getPath())) {return 0;}String gridName = values.getAsString(KEY_NAME);// Verify that this is a valid grid optionGridOption match = null;for (GridOption option : parseAllGridOptions()) {if (option.name.equals(gridName)) {match = option;break;}}if (match == null) {return 0;}InvariantDeviceProfile.INSTANCE.get(getContext()).setCurrentGrid(getContext(), gridName);return 1;}

然后会走到InvariantDeviceProfile的setCurrentGrid方法;下一步走到getPredefinedDeviceProfiles(Context context, String gridName)

 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {ArrayList<DisplayOption> profiles = new ArrayList<>();try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {final int depth = parser.getDepth();int type;while (((type = parser.next()) != XmlPullParser.END_TAG ||parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {if ((type == XmlPullParser.START_TAG)&& GridOption.TAG_NAME.equals(parser.getName())) {GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));...

device_profiles

<profiles xmlns:launcher="http://schemas.android.com/apk/res-auto" ><grid-optionlauncher: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-optionlauncher:name="Super Short Stubby"launcher:minWidthDps="255"launcher:minHeightDps="300"launcher:iconImageSize="48"launcher:iconTextSize="13.0"launcher:canBeDefault="true" /><display-optionlauncher:name="Shorter Stubby"launcher:minWidthDps="255"launcher:minHeightDps="400"launcher:iconImageSize="48"launcher:iconTextSize="13.0"launcher:canBeDefault="true" /></grid-option>

根据我们设置的grid_options名字匹配默认加载的布局文件

Android11 Launcher添加网格布局设置(3x3,4x4,5x5)相关推荐

  1. android动态添加网格布局,android – 动态网格布局

    您可以动态生成GridView. GridView将根据您的需要包含ImageView和TextView.您必须使用自定义适配器.在它的getView方法中,填充ImageView和TextView. ...

  2. css grid网格布局设置行间距和列间距

    grid-gap:15px 13px; //行间距 列间距

  3. java:布局方法(网格布局)

    网格布局 一.简单说明 二.关键代码 三.流程图 四.例子说明 1. 有17个"按钮"排列 (1)源码A (2)运行效果 2. 有36个"按钮"排列 (1)源码 ...

  4. 【鸿蒙 HarmonyOS】UI 布局 ( 网格布局 TableLayout )

    文章目录 一.网格布局 TableLayout 一.网格布局 TableLayout 网格布局 需要设置整个布局中有多少行 , 多少列 , 每个单元格都可以设置一个组件 , 这个组件可以是单个 , 也 ...

  5. 网格布局java_Java 网格布局

    /* * 网络布局 * */ package com.swing; import java.awt.GridLayout; import javax.swing.JButton; import jav ...

  6. 一个设置容器和网格布局的小技巧

    查看bootstrap实现网格布局的源码,可以发现: 网格容器(类属性.container或者.container-fluid标识的元素),网格中的行(.row标识的元素)都包括了一个和类属性.cle ...

  7. jpanel网格布局添加滚动条_jpanel 上下布局

    [解析] 本题考查Java中的布局管理器.FlowLayout是Pane和Applet默认的布局管理器,构件在容器中从上到下.从左到右进行放置,所以选项C为正确答案.BorderLayout是Wi.. ...

  8. android网格布局间隙,RecyclerView网格布局瀑布流布局设置间距

    RecyclerView在网格布局或者瀑布流布局下,如果要设置间距,可以使用ItemDecoration. 下面的代码是设置显示两列数据RecyclerView的情况. cat.png @Overri ...

  9. css grid设置宽度,如何设置Grid Layout (网格布局)列的最小宽度

    如何设置Grid Layout (网格布局)列的最小宽度?设置网格的列最小宽度时,在网格的列宽设置部分中使用minmax()函数,下面我们就来看具体的内容. 我们先来看一下minmax()函数的格式m ...

最新文章

  1. 库存事务处理现有量检查
  2. NETCONF 环境搭建
  3. 【深度学习】Pytorch的深度神经网络剪枝应用
  4. Linux脚本关联,shell数组和关联数组
  5. python高阶函数filter_python 高阶函数之filter
  6. Redis命令参考简体中文版
  7. 启明云端分享| 图文+实操+视频,手把手教你Eclipse搭建esp-idf环境
  8. HTTP学习笔记:HTTP的消息结构
  9. word样式基准_「word技巧」简单的排版技巧—给word文档添加各种样式边框线
  10. AS(Android studio)常见问题
  11. 使用 BenchmarkDotnet 测试代码性能
  12. Oracle Weblogic 11g(10.3.4)的小知识
  13. 组合数学 - 组合数的个数
  14. glibc版本查看_[译] 写一个简单的内存分配器(替换glibc中的malloc函数)
  15. 【D】分布式系统的CAP理论
  16. a5松下驱动器参数设置表_松下伺服驱动器参数设置MSD043A1X
  17. 窗体全部透明,控件不透明
  18. 题目名称:你好,i春秋
  19. 番茄花园 产业链 洪磊 中国
  20. 微信公众平台的开发流程及其要点

热门文章

  1. git基础之切换分支
  2. 啥是数据处理能力?(二)数据处理工具
  3. 使用xiaopiu常见技巧
  4. 五、Lua 变量的学习
  5. Linux目录配置与FHS标准
  6. GLSL vs HLSL vs Cg
  7. JavaScript中pop() 方法
  8. 服务器系统如何更新补丁,服务器更新操作系统补丁
  9. mysql的grant用法
  10. python 线程 (概念+示例代码)