简体   繁体   中英

Get access to application layout from Android Phonegap Plugin

I'm currently writing a plugin that is attempting to add a view to the current LinearLayout of my application.

Though I am struggling to get access to the linear layout from within a plugin, I can add the view fine if I do so within my main activity as so:

MyView view = new MyView(this);
root.addView(myView);

But to get the root LinearLayout in my plugin I have assumed that:

this.cordova.getActivity();

is my main activity and have been trying to cast it to the type of my main activity and call a function I added that will return the root LinearLayout object as so:

MyActivity myAct = (MyActivity)this.cordova.getActivity();

MyView view = new MyView(myAct);
myAct .GetLinearLayout().addView(view);

Though this doesn't seem to work and I receive no errors or such to help figure out why?

Anyone know how I can get access to the layout to add my view?

Okay so I solved this slightly differently to what I was attempting to do above.

Firstly casting the activity returned by cordova:

MyActivity myAct = (MyActivity)this.cordova.getActivity();

Does actually work and returns the instance of your main activity, so that wasn't the problem.

In the end I couldn't figure out why adding another view to the root cordova layout from the main activity worked but not when I did so in a plugin so what I did was to create my view from within the activities onCreate() then I provided an accessor to the view class back to my plugin and worked on from there.

The layout no longer exists after init() completes. The last thing that happens in init() is setContentView() is called with the layout. When setContentView() is called with a layout, the layout is inflated and the individual views in the layout are added to the activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM