gpt4 book ai didi

android - 单击抽屉导航项目上的更改屏幕

转载 作者:行者123 更新时间:2023-11-29 20:06:18 25 4
gpt4 key购买 nike

我是纯 Android 的新手,来自 Xamarin.Forms,所以这里是非常基本的问题。我使用抽屉导航模板开始了我的项目,并且我试图更改在我选择其中一个抽屉导航项目时默认看到的“hello world”屏幕。我看到 content_main.xml 中有一个定义“Hello World”屏幕的相对布局。如何将我看到的屏幕切换到另一个 xml 布局?像这样

    public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.screen1) {
// set current screen to content_main.xml
} else if (id == R.id.screen2) {
// set current screen to my_new_screen.xml
} else if (id == R.id.screen3) {
// set current screen to my_other_new_screen.xml
}

最佳答案

您可以像这样在项目点击上启动新的 Activity

Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
startActivity(intent);

或者如果您正在使用 fragment ,您可以通过此代码更改 fragment

Fragment newFragment = new YourNewFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container_name, newFragment);
transaction.commit();

关于android - 单击抽屉导航项目上的更改屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35619887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com