android去抽屉式桌面默认应用排序

android 8.1高通8909平台桌面默认原生应用Launcher3,行业机不需要抽屉式样式,去抽屉式的方式网上有很多,在此不再赘述。

设备配置文件

大众机型配置,涵盖各种屏幕大小,根据屏幕大小匹配合适的行列,其中就有默认的排列顺序文件,由标签launcher:defaultLayoutId选择。

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/com.android.launcher3" >

    <profile
        launcher:name="Super Short Stubby"
        launcher:minWidthDps="255"
        launcher:minHeightDps="300"
        launcher:numRows="2"
        launcher:numColumns="3"
        launcher:numFolderRows="2"
        launcher:numFolderColumns="3"
        launcher:minAllAppsPredictionColumns="3"
        launcher:iconSize="48"
        launcher:iconTextSize="13.0"
        launcher:numHotseatIcons="3"
        launcher:defaultLayoutId="@xml/default_workspace_3x3"
        />

    <profile
        launcher:name="Shorter Stubby"
        launcher:minWidthDps="255"
        launcher:minHeightDps="400"
        launcher:numRows="3"
        launcher:numColumns="3"
        launcher:numFolderRows="3"
        launcher:numFolderColumns="3"
        launcher:minAllAppsPredictionColumns="3"
        launcher:iconSize="48"
        launcher:iconTextSize="13.0"
        launcher:numHotseatIcons="3"
        launcher:defaultLayoutId="@xml/default_workspace_3x3"
        />

    <profile
        launcher:name="Short Stubby"
        launcher:minWidthDps="275"
        launcher:minHeightDps="420"
        launcher:numRows="3"
        launcher:numColumns="4"
        launcher:numFolderRows="3"
        launcher:numFolderColumns="4"
        launcher:minAllAppsPredictionColumns="4"
        launcher:iconSize="48"
        launcher:iconTextSize="13.0"
        launcher:numHotseatIcons="5"
        launcher:defaultLayoutId="@xml/default_workspace_4x4"
        />

    <profile
        launcher:name="Stubby"
        launcher:minWidthDps="255"
        launcher:minHeightDps="450"
        launcher:numRows="3"
        launcher:numColumns="4"
        launcher:numFolderRows="3"
        launcher:numFolderColumns="4"
        launcher:minAllAppsPredictionColumns="4"
        launcher:iconSize="48"
        launcher:iconTextSize="13.0"
        launcher:numHotseatIcons="5"
        launcher:defaultLayoutId="@xml/default_workspace_4x4"
        />

    <profile
        launcher:name="Nexus S"
        launcher:minWidthDps="296"
        launcher:minHeightDps="491.33"
        launcher:numRows="4"
        launcher:numColumns="4"
        launcher:numFolderRows="4"
        launcher:numFolderColumns="4"
        launcher:minAllAppsPredictionColumns="4"
        launcher:iconSize="48"
        launcher:iconTextSize="13.0"
        launcher:numHotseatIcons="5"
        launcher:defaultLayoutId="@xml/default_workspace_4x4"
        />

    <profile
        launcher:name="Nexus 4"
        launcher:minWidthDps="359"
        launcher:minHeightDps="567"
        launcher:numRows="4"
        launcher:numColumns="4"
        launcher:numFolderRows="4"
        launcher:numFolderColumns="4"
        launcher:minAllAppsPredictionColumns="4"
        launcher:iconSize="54"
        launcher:iconTextSize="13.0"
        launcher:numHotseatIcons="5"
        launcher:defaultLayoutId="@xml/default_workspace_4x4"
        />

    <profile
        launcher:name="Nexus 5"
        launcher:minWidthDps="335"
        launcher:minHeightDps="567"
        launcher:numRows="4"
        launcher:numColumns="4"
        launcher:numFolderRows="4"
        launcher:numFolderColumns="4"
        launcher:minAllAppsPredictionColumns="4"
        launcher:iconSize="54"
        launcher:iconTextSize="13.0"
        launcher:numHotseatIcons="5"
        launcher:defaultLayoutId="@xml/default_workspace_4x4"
        />

    <profile
        launcher:name="Large Phone"
        launcher:minWidthDps="406"
        launcher:minHeightDps="694"
        launcher:numRows="5"
        launcher:numColumns="5"
        launcher:numFolderRows="4"
        launcher:numFolderColumns="4"
        launcher:minAllAppsPredictionColumns="4"
        launcher:iconSize="56"
        launcher:iconTextSize="14.4"
        launcher:numHotseatIcons="5"
        launcher:defaultLayoutId="@xml/default_workspace_5x5"
        />

    <profile
        launcher:name="Nexus 7"
        launcher:minWidthDps="575"
        launcher:minHeightDps="904"
        launcher:numRows="5"
        launcher:numColumns="6"
        launcher:numFolderRows="4"
        launcher:numFolderColumns="5"
        launcher:minAllAppsPredictionColumns="4"
        launcher:iconSize="64"
        launcher:iconTextSize="14.4"
        launcher:numHotseatIcons="7"
        launcher:defaultLayoutId="@xml/default_workspace_5x6"
        />

    <profile
        launcher:name="Nexus 10"
        launcher:minWidthDps="727"
        launcher:minHeightDps="1207"
        launcher:numRows="5"
        launcher:numColumns="6"
        launcher:numFolderRows="4"
        launcher:numFolderColumns="5"
        launcher:minAllAppsPredictionColumns="4"
        launcher:iconSize="76"
        launcher:iconTextSize="14.4"
        launcher:numHotseatIcons="7"
        launcher:defaultLayoutId="@xml/default_workspace_5x6"
        />

    <profile
        launcher:name="20-inch Tablet"
        launcher:minWidthDps="1527"
        launcher:minHeightDps="2527"
        launcher:numRows="7"
        launcher:numColumns="7"
        launcher:numFolderRows="6"
        launcher:numFolderColumns="6"
        launcher:minAllAppsPredictionColumns="4"
        launcher:iconSize="100"
        launcher:iconTextSize="20.0"
        launcher:numHotseatIcons="7"
        launcher:defaultLayoutId="@xml/default_workspace_5x6"
        />

</profiles>

如何选择合适的设备配置文件

InvariantDeviceProfile根据获取的最小w x h在device_profiles.xml中选择最接近数值的profile进行初始化。

packages/apps/Launcher3/src/com/android/launcher3/InvariantDeviceProfile.java

InvariantDeviceProfile(Context context) {
...
		// This guarantees that width < height
        minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
        minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);

        ArrayList<InvariantDeviceProfile> closestProfiles = findClosestDeviceProfiles(
                minWidthDps, minHeightDps, getPredefinedDeviceProfiles(context));
        InvariantDeviceProfile interpolatedDeviceProfileOut =
                invDistWeightedInterpolate(minWidthDps,  minHeightDps, closestProfiles);
...

	ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles(Context context) {
        ArrayList<InvariantDeviceProfile> 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) && "profile".equals(parser.getName())) {
                    TypedArray a = context.obtainStyledAttributes(
                            Xml.asAttributeSet(parser), R.styleable.InvariantDeviceProfile);
                    int numRows = a.getInt(R.styleable.InvariantDeviceProfile_numRows, 0);
                    int numColumns = a.getInt(R.styleable.InvariantDeviceProfile_numColumns, 0);
                    float iconSize = a.getFloat(R.styleable.InvariantDeviceProfile_iconSize, 0);
                    profiles.add(new InvariantDeviceProfile(
                            a.getString(R.styleable.InvariantDeviceProfile_name),
                            a.getFloat(R.styleable.InvariantDeviceProfile_minWidthDps, 0),
                            a.getFloat(R.styleable.InvariantDeviceProfile_minHeightDps, 0),
                            numRows,
                            numColumns,
                            a.getInt(R.styleable.InvariantDeviceProfile_numFolderRows, numRows),
                            a.getInt(R.styleable.InvariantDeviceProfile_numFolderColumns, numColumns),
                            a.getInt(R.styleable.InvariantDeviceProfile_minAllAppsPredictionColumns, numColumns),
                            iconSize,
                            a.getFloat(R.styleable.InvariantDeviceProfile_landscapeIconSize, iconSize),
                            a.getFloat(R.styleable.InvariantDeviceProfile_iconTextSize, 0),
                            a.getInt(R.styleable.InvariantDeviceProfile_numHotseatIcons, numColumns),
                            a.getResourceId(R.styleable.InvariantDeviceProfile_defaultLayoutId, 0),
                            a.getResourceId(R.styleable.InvariantDeviceProfile_demoModeLayoutId, 0)));
                    a.recycle();
                }
            }
        } catch (IOException|XmlPullParserException e) {
            throw new RuntimeException(e);
        }
        return profiles;
    }
    ...
}

odm

我们可以在device_profiles.xml中添加最最精准的屏幕尺寸作为定制设备的配置文件,因为配置文件中的尺寸肯定有所差异,才使用最近似值来选择,我们有设备就能配置更加接近的数值。
之后定义属于我们的排序文件:launcher:defaultLayoutId="@xml/default_workspace_4x3"

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2009 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.
-->

<favorites xmlns:launcher="http://schemas.android.com/apk/res-auto/com.android.launcher3">
	<!-- screen [0] -->
	<!-- first row -->
	<favorite
        launcher:packageName=""
        launcher:className=""
        launcher:screen="0"
        launcher:x="0"
        launcher:y="0" />
        
    <favorite
        launcher:packageName=""
        launcher:className=""
        launcher:screen="0"
        launcher:x="1"
        launcher:y="0" />
        
    <favorite
        launcher:packageName=""
        launcher:className=""
        launcher:screen="0"
        launcher:x="2"
        launcher:y="0" />
        
    <!-- second row -->
	<favorite
        launcher:packageName=""
        launcher:className=""
        launcher:screen="0"
        launcher:x="0"
        launcher:y="1" />
        
    <favorite
        launcher:packageName=""
        launcher:className=""
        launcher:screen="0"
        launcher:x="1"
        launcher:y="1" />
        
    <favorite
        launcher:packageName=""
        launcher:className=""
        launcher:screen="0"
        launcher:x="2"
        launcher:y="1" />

	<!-- third row -->
	<favorite
        launcher:packageName=""
        launcher:className=""
        launcher:screen="0"
        launcher:x="0"
        launcher:y="2" />
        
    <favorite
        launcher:packageName=""
        launcher:className=""
        launcher:screen="0"
        launcher:x="1"
        launcher:y="2" />
        
    <favorite
        launcher:packageName=""
        launcher:className=""
        launcher:screen="0"
        launcher:x="2"
        launcher:y="2" />
</favorites>

默认排序用最原始的包名/类名来排列,x=0,y=0为屏幕左上角的第一个位置,以此类推。
具体的标签说明和位置信息请参考:android Launcher3 设置默认桌面应用

android8.1原生配置文件的写法是通过action + category的方式:

<resolve
        launcher:screen="0"
        launcher:x="0"
        launcher:y="-1" >
        <favorite launcher:uri="#Intent;action=android.intent.action.MAIN;category=android.intent.category.APP_EMAIL;end" />
        <favorite launcher:uri="mailto:" />
    </resolve>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 抽屉式布局是一种常用的 UI 设计模式,允许用户通过向左或向右滑动屏幕来打开或关闭一个侧边栏菜单。以下是一个简单的实现步骤: 1. 在 XML 布局文件中添加 DrawerLayout 和两个子视图:主视图和抽屉视图。 ``` <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent"/> <ListView android:id="@+id/drawer_list" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#ffffff"/> </android.support.v4.widget.DrawerLayout> ``` 2. 在 Java 代码中找到 DrawerLayout 和两个子视图的引用。 ``` DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); FrameLayout mainContent = (FrameLayout) findViewById(R.id.main_content); ListView drawerList = (ListView) findViewById(R.id.drawer_list); ``` 3. 创建一个 ArrayAdapter 以提供抽屉视图的数据。 ``` String[] menuItems = {"Item 1", "Item 2", "Item 3"}; ArrayAdapter<String> drawerListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuItems); drawerList.setAdapter(drawerListAdapter); ``` 4. 设置 ActionBarDrawerToggle 作为 DrawerLayout 的开关。 ``` ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close); drawerLayout.addDrawerListener(drawerToggle); drawerToggle.syncState(); ``` 5. 在 onOptionsItemSelected() 方法中调用 ActionBarDrawerToggle 的 onOptionsItemSelected() 方法。 ``` @Override public boolean onOptionsItemSelected(MenuItem item) { if (drawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } ``` 完成以上步骤后,就可以在应用程序中添加抽屉式布局了。用户可以通过向左或向右滑动屏幕来打开或关闭侧边栏菜单。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值