开发者

Switch between 2 layouts in Android

I have two layouts, what is the best way to switch between them programatically (not using an xml file).

开发者_StackOverflow

And how can I do it?


Basically there are two ways.

1.) you got one XML containing all components you want to use. The ones which are not available at the moment, should bis hidden. When the user should have the possibility to use them, just make them visible

2.) this is definitly the better solution because Android is concipated for this method. You have 2 Activities and 2 layout XML files. When you want to display another layout, start the second Activity.

in your first Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainlayout);

    Button btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(StackOverflowActivity.this, Login.class);
            startActivityForResult(i, LOGIN_REQUEST);
        }
    });

}

your second Activity;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜