0

I have a android project as a library, and this library contains a home screen. The new app needs this home screen (as well as everything else in the library), but but also needs to make changes to all of the graphics, as well as add a few new buttons, how would I do this?

I don't want to edit the library. I can make the new app have images with the same name as the library, so the new images over-take the libraries images. But this doesn't help me in adding in 3 new buttons to the layout, as well as the new pieces of functionality to the activity (while keeping all of the old functionality).

Any one have any experience with this sort of thing?

4

1 回答 1

1

Extend the home screen activity from the library. Override the onCreate(Bundle):

public class NewMainActivity extends HomeScreenActivityFromLibrary {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View viewFromLibrary = getLayoutInflater().inflate(
                                      library.package.name.R.layout.home_screen, null);

        // Make changes; add Buttons to `viewFromLibrary`
        // Add functionality to Buttons; setOnClickListeners

        setContentView(viewFromLibrary);
    }
}

Depending upon how your HomeScreenActivityFromLibrary is setup, you may have to override additional methods. Another thing you can do is: simply copy the layout file used in HomeScreenActivityFromLibrary to your project's res/layout folder. Add additional widgets(buttons etc.) in the xml file. In this case, all you need to do is:

public class NewMainActivity extends HomeScreenActivityFromLibrary {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.newly_created_xml_file);

        Button b1 = (Button) findViewById(R.id.b1_id);
        ....
        ....
    }
}
于 2013-08-22T17:39:21.847 回答