如何在Android的JellyBean启动器中添加自定义视图 [英] How to add custom view in android's JellyBean Launcher

查看:85
本文介绍了如何在Android的JellyBean启动器中添加自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Android制作自定义启动器.我已经引用了Android的Jellybean启动器的代码.现在我想在此启动器中进行一些修改.

I am working on making custom launcher in android. I have referred the code of android's Jellybean launcher. now I want to make some modification in this launcher.

我想要的内容:众所周知,默认情况下有五个工作区屏幕,我想在任何一个工作区屏幕中添加自定义视图.我的xml文件应在任一屏幕中放大.

What I want : As we know there are default five work-space screens and I want to add custom view in any one of the workspace screen. My xml file should be inflated in any one of the screen.

我已经尝试了很多方法来实现它,但是由于默认的启动器代码非常复杂,因此仍然没有找到解决方法.

I have tried many ways to do it but as the default launcher code is very complex still having no luck to finding out way for it.

已经有一个名为 SOHO 在Playstore中完全可以满足我的需求.我已经添加了屏幕截图以引用我想要的内容.

There is already app named SOHO in Playstore doing exactly what I want. I have add the screenshot for referencing what i want.

如果您有任何想法要帮助我,请帮助我.

Please help me if anyone of you having any idea to do it.

推荐答案

我为您提供了答案.您可以从(AOSP)的 Launcher2 Launcher3 包中完成此操作.Jellybean使用的可能是 Launcher2 .我个人建议您使用 Launcher3 ,它具有内置的方式.

I've the answer for you. You can do it both in Launcher2 and Launcher3 package from (AOSP). Jellybean is using Launcher2 may be. I personally suggest you to go with Launcher3, it has buit-in way to do so.

创建一个扩展 com.android.launcher3.Launcher 类的类,并覆盖必要的方法,如下所示:

create a class that extends the com.android.launcher3.Launcher class and override the necessary methods like so:

public class MyLauncher extends Launcher {


    @Override
    protected boolean hasCustomContentToLeft() {
        return true;
    }


    @Override
    protected void addCustomContentToLeft() {
        View customView = getLayoutInflater().inflate(R.layout.custom, null);

        CustomContentCallbacks callbacks = new CustomContentCallbacks() {

            @Override
            public void onShow() {}

            @Override
            public void onScrollProgressChanged(float progress) {}

            @Override
            public void onHide() {}
        };


        addToCustomContentPage(customView, callbacks, "custom view");
    }

}

此处 R.layout.custom 是您想要的自定义视图.然后在清单文件中,将启动器活动类从 Launcher 更改为 MyLauncher .就是这样.

Here R.layout.custom is the custom view that you wanted. Then in the manifest file change the launcher activity class from Launcher to MyLauncher. And that's it.

创建以下方法:

public void addCustomView(View child){
   CellLayout layout = (CellLayout) getChildAt(0);
   layout.addView(child);
}

然后在 Launcher.java 中找到以下行:

mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);

然后将以下代码粘贴到该行之后的位置:

then paste the following code somewhere after that line:

View child = LayoutInflater.from(this).inflate(R.layout.custom, null);
mWorkspace.addCustomView(child);

这篇关于如何在Android的JellyBean启动器中添加自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆