我試圖弄清楚如何在應(yīng)用程序啟動(dòng)時(shí)更改加載的 xml。當(dāng)前它打開(kāi) activity_main.xml。有沒(méi)有辦法讓?xiě)?yīng)用程序打開(kāi)我的 info.xml(也在我的側(cè)邊導(dǎo)航欄中)而不創(chuàng)建新活動(dòng)?更改了 setcontentview 的主要活動(dòng)代碼:package com.example.cubehelppp;import android.os.Bundle;import androidx.fragment.app.FragmentManager;import com.google.android.material.navigation.NavigationView;import androidx.core.view.GravityCompat;import androidx.drawerlayout.widget.DrawerLayout;import androidx.appcompat.app.ActionBarDrawerToggle;import androidx.appcompat.app.AppCompatActivity;import androidx.appcompat.widget.Toolbar;import android.view.Menu;import android.view.MenuItem; public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.info); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this);}@Overridepublic void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); }}@Overridepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true;}
如何更改主要活動(dòng)中的起始 xml?
月關(guān)寶盒
2023-04-19 16:57:49