开发者

Android: Is it possible to make a Contentview dynamic?

开发者 https://www.devze.com 2023-01-25 15:17 出处:网络
Is it possible to create just one class and use a variable for contentview?I am thinking of creating a main layout with loads of buttons with IDs.Once a button is pressed it\'ll then pass the ID to th

Is it possible to create just one class and use a variable for contentview? I am thinking of creating a main layout with loads of buttons with IDs. Once a button is pressed it'll then pass the ID to the class and use that ID to open a specific Layout. One class, multiple layouts.

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.on开发者_StackOverflow中文版Create(savedInstanceState);
        setContentView(R.layout.main); //is it possible to make 'main' a variable?
    }
}


R.layout.main resolves to an integer (or maybe a long, I forget) so you should be able to set up some kind of control structure like so:

switch (mSomeDeterminingFactor) {
    case 1:
        layoutChoice = R.layout.main
        break;
    case 2:
        layoutChoice = R.layout.altMain1
        break;
    case 3:
        layoutChoice = R.layout.altMain2
        break;
    case else:
        layoutChoice = R.layout.main
        break;
 }
 setContentView(layoutChoice);

Edit: You should be careful how you use/save/store this number later though as you cannot guarantee the resource values will be the same. You should continue to reference them via the R class.

0

精彩评论

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

关注公众号