如何在所有活动中使用Android Studio默认导航抽屉[重复]

Posted

技术标签:

【中文标题】如何在所有活动中使用Android Studio默认导航抽屉[重复]【英文标题】:How to use Android Studio default Navigation Drawer in all Activities [duplicate] 【发布时间】:2016-03-19 00:10:21 【问题描述】:

如何在其他Activity中使用android默认Navigation?

我不想使用返回按钮。

主活动:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener 

TextView tvhead,tv6,tv7,tv8,tv9,tvbottom;
Typeface schriftart_courier_prime;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    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.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    FragmentManager fm = getFragmentManager();
    fm.beginTransaction().replace(R.id.content_frame, new MainFragment()).commit();


@Override
public void onBackPressed() 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) 
        drawer.closeDrawer(GravityCompat.START);
     else 
        super.onBackPressed();
    


@Override
public 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;


@Override
public boolean onOptionsItemSelected(MenuItem item) 
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_about) 

        Intent getNameScreenIntent2 = new Intent (this, AboutActivity.class);


        final int result = 1;

        startActivity(getNameScreenIntent2);

        return true;

        //Actionbar element für später
    else if (id == R.id.action_phone)

    

    return super.onOptionsItemSelected(item);



@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) 

    FragmentManager fm = getFragmentManager();

    int id = item.getItemId();

    if (id == R.id.nav_home) 
        fm.beginTransaction().replace(R.id.content_frame, new MainFragment()).commit();
     else if (id == R.id.nav_experiments) 

        Intent getNameScreenIntent = new Intent(this, ExperimentsList.class);

        final int result = 1;

        startActivity(getNameScreenIntent);
     else if (id == R.id.nav_website) 

        Intent getNameScreenIntent = new Intent(this, Web.class);

        final int result = 1;

        startActivity(getNameScreenIntent);
     else if (id == R.id.nav_share) 

        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("text/plain");
        share.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_text));
        startActivity(Intent.createChooser(share, getString(R.string.share_title)));
    

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;

【问题讨论】:

您能否通过将示例代码减少到演示问题所需的最低限度,或提供更多解释,或两者兼而有之,让人们更容易帮助您? 【参考方案1】:

让所有需要该抽屉的活动都继承自自定义抽象 Activity 类,该类将抽屉添加到其 onCreate 方法中。

创建一个类似的类:

public abstract class MyNavigationActivity extends AppCompatActivity  
   @Override 
   protected void onCreate(Bundle savedInstanceState)  
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     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.setDrawerListener(toggle);
     toggle.syncState();

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
     navigationView.setNavigationItemSelectedListener(this);
    
 

然后:

public class MyActivity extends MyNavigationActivity 
   @Override 
   protected void onCreate(Bundle savedInstanceState)  
       super.onCreate(savedInstanceState);
       FragmentManager fm = getFragmentManager();
       fm.beginTransaction().replace(R.id.content_frame, new MainFragment()).commit();
   
 

【讨论】:

谢谢回答,但自定义摘要是什么意思? 请举个例子 你能用我的代码做这个例子吗?这更好理解:D 我是 Android 编程新手 @FlorianReich 请阅读抽象类和方法:tutorialspoint.com/java/java_abstraction.htm

以上是关于如何在所有活动中使用Android Studio默认导航抽屉[重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Android Studio 的登录活动模板中实现 AsyncTask

我在所有活动和mainfest中的“this.something”中出现错误,android studio的xml

如何在 Android Studio 中使用按钮按初始筛选并使用 SQLite 将其显示在另一个活动上

如何将值从活动传递到 Android Studio 中的类?

如何在 Kotlin 中创建一个打开新活动(Android Studio)的按钮?

如何在进入不同活动的情况下不删除 Android Studio 活动中的变量?