Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 所有活动中的导航抽屉_Android_Xml_Navigation - Fatal编程技术网

Android 所有活动中的导航抽屉

Android 所有活动中的导航抽屉,android,xml,navigation,Android,Xml,Navigation,我正在使用导航抽屉的导航视图…抽屉中有某些菜单项,当我单击某个特定活动时,会打开一个新活动,我也想从该活动中打开导航抽屉。。我尝试了一种方法,将我的主要活动(具有抽屉的活动)扩展到活动(或第二个活动)…并将其添加到第二个活动中,而不是setcontentview。。但问题是我的第二个活动xml就在mainactivity.xml之上。。我看到了两个活动(mainactivity.xml上方的secondactivity)如何仅显示抽屉中的secondactivity LayoutInflater

我正在使用导航抽屉的导航视图…抽屉中有某些菜单项,当我单击某个特定活动时,会打开一个新活动,我也想从该活动中打开导航抽屉。。我尝试了一种方法,将我的主要活动(具有抽屉的活动)扩展到活动(或第二个活动)…并将其添加到第二个活动中,而不是setcontentview。。但问题是我的第二个活动xml就在mainactivity.xml之上。。我看到了两个活动(mainactivity.xml上方的secondactivity)如何仅显示抽屉中的secondactivity

LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View activityView = layoutInflater.inflate(R.layout.activity_credits, null,false);
        // add the custom layout of this activity to frame layout.
        drawerLayout.addView(activityView);

您可以通过创建一个公共类BaseActivity.java来完成此任务,并将导航抽屉的代码放在这个类中。并使用BaseActivity.java扩展类,在其中显示导航抽屉

public class BaseActivity extends AppCompatActivity  {

    public Toolbar toolbar;                              // Declaring the Toolbar Object


    ActionBarDrawerToggle mDrawerToggle;
    Context context;
    private NavigationView navigationView;
    private DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    protected boolean useToolbar() {
        return true;
    }


    @Override
    public void setContentView(int layoutResID) {
        context = this;

        DrawerLayout fullView = (DrawerLayout) getLayoutInflater().inflate(R.layout.drawer_main, null);
        FrameLayout activityContainer = (FrameLayout) fullView.findViewById(R.id.frame);
        getLayoutInflater().inflate(layoutResID, activityContainer, true);

        super.setContentView(fullView);
        toolbar = (Toolbar) fullView.findViewById(R.id.tool_bar);

        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("");
        toolbar.setTitle("");
        this.getSupportActionBar().setElevation(0);

        getSupportActionBar().setLogo(R.drawable.ic_arrahm);
        //  toolbar.setLogo(R.drawable.ic_main);
        if (useToolbar()) {
            setSupportActionBar(toolbar);
            setTitle("Places Near Me");
        } else {
            toolbar.setVisibility(View.GONE);
        }


        //Initializing NavigationView
        navigationView = (NavigationView) findViewById(R.id.navigation_view);

        //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            // This method will trigger on item Click of navigation menu
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                //Checking if the item is in checked state or not, if not make it in checked state
                if (menuItem.isChecked()) menuItem.setChecked(false);
                else menuItem.setChecked(true);

                //Closing drawer on item click
                drawerLayout.closeDrawers();

                //Check to see which item was being clicked and perform appropriate action
                switch (menuItem.getItemId()) {
                    case R.id.edit_profile:                   
                        return true;
                    case R.id.change_password:
                        return true;
                    default:
                        Toast.makeText(getApplicationContext(), "Work in progress", Toast.LENGTH_SHORT).show();
                        return true;
                }
            }
        });

        // Initializing Drawer Layout and ActionBarToggle
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        View header = navigationView.getHeaderView(0);
        TextView tvName = (TextView) header.findViewById(R.id.name);
        TextView tvEmail = (TextView) header.findViewById(R.id.email);
        String name = Preferences.getDataFromStringPreferences(context,Constants.USER_DETAILS, Constants.USER_NAME);

        if (name != null) {
            tvName.setText(name);
        }


        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer) {

            @Override
            public void onDrawerClosed(View drawerView) {
                // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
        };

        //Setting the actionbarToggle to drawer layout
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        //calling sync state is necessay or else your hamburger icon wont show up
        actionBarDrawerToggle.syncState();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        return mDrawerToggle.onOptionsItemSelected(item);
    }
}
创建drawer_main.xml布局文件

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/tool_bar"
            layout="@layout/toolbar" />

        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </FrameLayout>

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer">


    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

在布局下创建toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    >
</android.support.v7.widget.Toolbar>

创建drawer_header.xml布局资源文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="178dp"
    android:background="@color/colorPrimary"
    android:orientation="vertical"
    android:padding="20dp"
    android:weightSum="1">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:text=""
            android:textColor="#ffffff"
            android:textSize="14sp"
            android:textStyle="bold"

            />

        <TextView
            android:id="@+id/email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:text=""

            android:textColor="#ffffff"
            android:textSize="14sp"
            android:textStyle="normal"

            />
    </LinearLayout>

    <ImageView
        android:id="@+id/circleView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_centerInParent="true"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="30dp" />
</RelativeLayout>

您可以通过创建一个公共类(如BaseActivity.java)并将导航抽屉的代码放入该类中来完成此任务。并使用BaseActivity.java扩展类,在其中显示导航抽屉

public class BaseActivity extends AppCompatActivity  {

    public Toolbar toolbar;                              // Declaring the Toolbar Object


    ActionBarDrawerToggle mDrawerToggle;
    Context context;
    private NavigationView navigationView;
    private DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    protected boolean useToolbar() {
        return true;
    }


    @Override
    public void setContentView(int layoutResID) {
        context = this;

        DrawerLayout fullView = (DrawerLayout) getLayoutInflater().inflate(R.layout.drawer_main, null);
        FrameLayout activityContainer = (FrameLayout) fullView.findViewById(R.id.frame);
        getLayoutInflater().inflate(layoutResID, activityContainer, true);

        super.setContentView(fullView);
        toolbar = (Toolbar) fullView.findViewById(R.id.tool_bar);

        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("");
        toolbar.setTitle("");
        this.getSupportActionBar().setElevation(0);

        getSupportActionBar().setLogo(R.drawable.ic_arrahm);
        //  toolbar.setLogo(R.drawable.ic_main);
        if (useToolbar()) {
            setSupportActionBar(toolbar);
            setTitle("Places Near Me");
        } else {
            toolbar.setVisibility(View.GONE);
        }


        //Initializing NavigationView
        navigationView = (NavigationView) findViewById(R.id.navigation_view);

        //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            // This method will trigger on item Click of navigation menu
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                //Checking if the item is in checked state or not, if not make it in checked state
                if (menuItem.isChecked()) menuItem.setChecked(false);
                else menuItem.setChecked(true);

                //Closing drawer on item click
                drawerLayout.closeDrawers();

                //Check to see which item was being clicked and perform appropriate action
                switch (menuItem.getItemId()) {
                    case R.id.edit_profile:                   
                        return true;
                    case R.id.change_password:
                        return true;
                    default:
                        Toast.makeText(getApplicationContext(), "Work in progress", Toast.LENGTH_SHORT).show();
                        return true;
                }
            }
        });

        // Initializing Drawer Layout and ActionBarToggle
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        View header = navigationView.getHeaderView(0);
        TextView tvName = (TextView) header.findViewById(R.id.name);
        TextView tvEmail = (TextView) header.findViewById(R.id.email);
        String name = Preferences.getDataFromStringPreferences(context,Constants.USER_DETAILS, Constants.USER_NAME);

        if (name != null) {
            tvName.setText(name);
        }


        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer) {

            @Override
            public void onDrawerClosed(View drawerView) {
                // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
        };

        //Setting the actionbarToggle to drawer layout
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        //calling sync state is necessay or else your hamburger icon wont show up
        actionBarDrawerToggle.syncState();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        return mDrawerToggle.onOptionsItemSelected(item);
    }
}
创建drawer_main.xml布局文件

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/tool_bar"
            layout="@layout/toolbar" />

        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </FrameLayout>

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer">


    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

在布局下创建toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    >
</android.support.v7.widget.Toolbar>

创建drawer_header.xml布局资源文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="178dp"
    android:background="@color/colorPrimary"
    android:orientation="vertical"
    android:padding="20dp"
    android:weightSum="1">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:text=""
            android:textColor="#ffffff"
            android:textSize="14sp"
            android:textStyle="bold"

            />

        <TextView
            android:id="@+id/email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:text=""

            android:textColor="#ffffff"
            android:textSize="14sp"
            android:textStyle="normal"

            />
    </LinearLayout>

    <ImageView
        android:id="@+id/circleView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_centerInParent="true"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="30dp" />
</RelativeLayout>


当您单击主活动中的抽屉时,它会打开一个新活动还是只显示一个片段?会打开一个新活动(例如secondactivity)您是否尝试过设置内容视图而不是将其膨胀?启动新活动时,不能将其充气,应使用setcontentview。编辑:为什么不使用片段?它在整个导航过程中保持相同的导航抽屉我尝试设置contentview,但抽屉未打开。。事实上,我已经搜索了很多,解决办法是必须膨胀它。。。我只是没有使用片段,现在我正在我的项目中..我现在不能切换到片段..我只是忘了使用片段…所以在这方面有什么帮助吗???所以这是老方法,但有一点调整,你会成功的:一个主活动有一个抽屉并调用多个片段。当你点击主活动中的抽屉时,它会打开一个新的活动还是仅仅显示一个片段?打开一个新的活动(比如说第二个活动)你试过设置内容视图而不是膨胀它吗?启动新活动时,不能将其充气,应使用setcontentview。编辑:为什么不使用片段?它在整个导航过程中保持相同的导航抽屉我尝试设置contentview,但抽屉未打开。。事实上,我已经搜索了很多,解决办法是必须膨胀它。。。我只是没有使用片段,现在我正在我的项目中..我现在不能切换到片段..我只是忘记了使用片段…所以在这方面有什么帮助吗???这是老办法,但稍加调整,你就会达到目的:它完成了一个主要活动,它有一个抽屉并调用多个片段