单击导航抽屉中的项目时启动新布局

时间:2017-08-10 19:47:30

标签: android android-layout

我有一个MainActivity.java和NavigationDrawer。我想点击navdrawer中的项目,开始新的布局。我知道我可以开始新的活动,但我不想要一个新的活动。新的布局适合我。这可能吗???

这是我的ActivityMain.java:

public class MainActivity extends AppCompatActivity {

public Toolbar toolbar;
protected DrawerLayout drawerLayout;
NavigationView navigationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    drawerLayout = (DrawerLayout) findViewById(R.id.a1);

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_more_vert_black_24dp);
    navigationView = (NavigationView) findViewById(R.id.a2);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            item.setChecked(true);
            drawerLayout.closeDrawer(Gravity.RIGHT);

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.khat) {
        if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
            drawerLayout.closeDrawer(Gravity.RIGHT);
        }

        else {
            drawerLayout.openDrawer(Gravity.RIGHT);
        }
        return super.onOptionsItemSelected(item);
    }
    else {
        Toast.makeText(getApplicationContext(),"SOMETHING",Toast.LENGTH_LONG).show();
    }


    return true;
}

public void onBackPressed(){
    if (drawerLayout.isDrawerOpen(Gravity.RIGHT)){
        drawerLayout.closeDrawer(Gravity.RIGHT);
    }else {super.onBackPressed();}

}
}

这是我想要在点击项目时开始的第二个布局:`

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#132740"
android:layout_height="match_parent">

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="20dp"
        android:textColor="#FFFFFF"
        android:id="@+id/textView"
        android:textSize="22sp"
        android:layout_marginTop="20dp"
        android:text="blah blah"/>
</RelativeLayout>

这可能吗?

2 个答案:

答案 0 :(得分:0)

如果只是你想改变的布局,有一个非常简单的解决方案:

if (Case_A)
  setContentView(R.layout.layout1);

else if (Case_B)
  setContentView(R.layout.layout2);

但考虑使用碎片:

https://developer.android.com/guide/components/fragments.html

对于每个布局,使用片段,您将能够将代码划分为多个类。它将更具可读性和轻量级:

Here is an example

答案 1 :(得分:0)

使用片段以使用不同操作的多个布局。如果您只想更改布局,那么您可以使用setContentView(新布局)在项目点击上设置新布局;