开发者

creating a xml layout from a custom layout. is it possible?

MainActivity.java:

public class MainActi开发者_开发问答vity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setTitle(R.string.app_name);
        setContentView(new SampleView(this));
    }
}

SampleView.java:

public class SampleView extends View {

    @Override
    protected void onDraw(Canvas canvas) {
        if (certaincondition = true) {
            //add elements to canvas etc
        } else {
            //How do I do the below? The layout is defined in xml. 
            //I do not want to use Intent. Please help me

            //create a layout from resource R.layout.idAbout and transfer control.
         } 
    }
}


Use a layout inflater:

View newRootViewElement;
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
newRootViewElement= li.inflate(R.layout.idAbout, null);


You can inflate a layout using

View.inflate(getContext(), R.layout.idAbout, viewParent);

where viewParent is a ViewParent that will be the parent of the inflated view (and can be null).

But what are you trying to do? It's more than a little odd to start a new activity or to modify the view hierarchy from within onDraw(). You might want to post a runnable to a Handler that will do what you want on the next cycle of the event loop. To start a new activity (such as displaying “About” info for the app) you should take a look at the Intent class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜