How to add custom view in android's JellyBean Launcher

*爱你&永不变心* 提交于 2019-12-20 11:06:14

问题


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.

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.

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.


回答1:


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.

Launcher3:

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

}

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.

Launcher2:

in Workspace.java create the following method:

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

then in Launcher.java, find the following line:

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



回答2:


If I remember correctly you just need to implement a standard activity which displays a home launcher. In your Manifest.xml you just need to define it like this:

<activity android:name=".YourLauncher" android:label="@string/launcher_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>



回答3:


you can simply add view in default lanucher use code

wm = (WindowManager) getSystemService("window");
params = new LayoutParams();
params.type = LayoutParams.TYPE_PHONE;
    params.format = PixelFormat.RGBA_8888;
    params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
            | LayoutParams.FLAG_NOT_FOCUSABLE;
    params.x = 100;
    params.y = 100;
    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
    params.width = WindowManager.LayoutParams.WRAP_CONTENT;
    params.gravity = Gravity.LEFT | Gravity.TOP;
wm.addView(view, params);

when you want to remove it just

wm.removeView(v);

you also need permission

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />



回答4:


Good news, not so good news, bad news.

Good new first.
It is possible to do what you want.

Now the not so good news.
You will have to write the launcher application from scratch(aka Home Screen). Yep, that involves doing all those nice and nifty things that the default launcher does(multiple pages, drag and drop, delete/add app icons, etc). Fortunately, its not as difficult as it sounds. Because the default launcher app itself is opensource. Though this code is complete, its not easy to read. A easier place to start would be the SDK

   Android-SDK/samples/android-x/Home/
   where x is the API level.

They have provided source code for an example home screen and it should give you a good start. With some perseverance and coffee, you should be able to modify the Launcher2 code to add a customized page of your own.

Now the Hard part.
Because a part of your goal is to keep the existing pages same and add a new page, getting this to work for all the flavors of android... HTC sense, Samsung TouchWiz, etc, etc is not a single person workload. They all have different features for the Home screen. Preserving these features and adding a new customized page is a tough task.



来源:https://stackoverflow.com/questions/17141113/how-to-add-custom-view-in-androids-jellybean-launcher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!