导航菜单在Child Activity中不起作用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了导航菜单在Child Activity中不起作用相关的知识,希望对你有一定的参考价值。

所以我做了一个菜单课,现在我尝试扩展我的其他活动,我得到按钮,但幻灯片不在其他活动中工作,但它在左上方的apears ...以免转到代码

这是我的xml activity_navigatoin_menu

   <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="ar.com.puebloyreforma.pyr.NavigationMenu">



        <android.support.design.widget.NavigationView
            app:headerLayout="@layout/menuheader"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/caldroid_black"
            app:itemTextColor="@color/white"
            app:itemIconTint="@color/white"
            app:menu="@menu/drawermenu"
            android:layout_gravity="start"
            >    


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

这是我的manudrawer

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:id="@+id/noticias" android:title="Noticias" android:icon="@drawable/iconews"></item>
    <item android:id="@+id/calendario" android:title="Calendario" android:icon="@drawable/icocalen"></item>
    <item android:id="@+id/sysacad" android:title="Sysacad" android:icon="@drawable/icosysa"></item>
    <item android:id="@+id/contacto" android:title="Contacto" android:icon="@drawable/icocon"></item>
    <item android:id="@+id/reglamento" android:title="Reglamento" android:icon="@drawable/icoreg"></item>

</menu>

在这里我的活动

package ar.com.puebloyreforma.pyr;

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;

public class NavigationMenu extends AppCompatActivity {
    private DrawerLayout mDL ;
    private ActionBarDrawerToggle mT  ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_menu);
        mDL = (DrawerLayout) findViewById(R.id.drawer) ;
        mT = new ActionBarDrawerToggle(this  , mDL ,R.string.open , R.string.close) ;
        mDL.addDrawerListener(mT);
        mT.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mT.onOptionsItemSelected(item)){
            return true ;
        }
        return super.onOptionsItemSelected(item);
    }
}

这是一个孩子的活动

package ar.com.puebloyreforma.pyr;

import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

public class PdfsActivity extends NavigationMenu {
    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public String target ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pdfs);



        Button tt , tm , tn, reg ;
        tt = (Button)this.findViewById(R.id.tt);
        tm = (Button)this.findViewById(R.id.tm);
        tn = (Button)this.findViewById(R.id.tn);
        reg = (Button)this.findViewById(R.id.reg);

        final String pdfs[] = {"tm" ,
                "tt" ,
                "tn",
                "Ord1549"
        } ;

        tm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                setTarget(pdfs[0]);
                Intent hola = new Intent (PdfsActivity.this,ReglamentoActivity.class);
                hola.putExtra("TARGET", getTarget());
                startActivity(hola);
            }
        });



        tt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                setTarget(pdfs[1]);
                Intent hola = new Intent (PdfsActivity.this,ReglamentoActivity.class);
                hola.putExtra("TARGET", getTarget());
                startActivity(hola);
            }
        });

        tn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                setTarget(pdfs[2]);
                Intent hola = new Intent (PdfsActivity.this,ReglamentoActivity.class);
                hola.putExtra("TARGET", getTarget());
                startActivity(hola);
            }
        });
        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                setTarget(pdfs[3]);
                Intent hola = new Intent (PdfsActivity.this,ReglamentoActivity.class);
                hola.putExtra("TARGET", getTarget());
                startActivity(hola);
            }
        });





    }

有自己的布局

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="ar.com.puebloyreforma.pyr.PdfsActivity">



            <Button
            android:id="@+id/tm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="MAñANA"


            android:layout_marginRight="60dp"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="8dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintVertical_bias="0.758"
            android:layout_marginLeft="8dp"
            app:layout_constraintLeft_toLeftOf="parent" />

        <Button
            android:id="@+id/tt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TARDE"


            android:layout_marginBottom="8dp"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintVertical_bias="0.335"
            android:layout_marginRight="123dp"
            app:layout_constraintRight_toRightOf="parent" />

        <Button
            android:id="@+id/tn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="NOCHE"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="68dp"
            app:layout_constraintVertical_bias="0.572"
            app:layout_constraintLeft_toLeftOf="parent" />

        <Button
            android:id="@+id/reg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="REGLA"
            app:layout_constraintLeft_toRightOf="@+id/tn"
            android:layout_marginLeft="0dp"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="8dp"
            android:layout_marginRight="8dp"
            app:layout_constraintVertical_bias="0.572"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintHorizontal_bias="0.713" />
    </android.support.constraint.ConstraintLayout>
答案

让我发布我的代码,我设法在子活动中获取导航抽屉。

我的AppCompatActivity(家长活动qazxsw poi)

class

activity_navigation_drawer.xml(ParentActivity public abstract class MyAppCompatActivity extends BaseAppCompatActivity { @Override public void setContentView(@LayoutRes int layoutResID) { CoordinatorLayout activityParentBase = (CoordinatorLayout) getLayoutInflater().inflate(R.layout.activity_navigation_drawer, null); RelativeLayout content = (RelativeLayout) activityParentBase.findViewById(R.id.content); setContentView(activityParentBase); getLayoutInflater().inflate(layoutResID, content, true); super.setContentView(activityParentBase); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle(""); setSupportActionBar(toolbar); final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( MyAppCompatActivity.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(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { int id = item.getItemId(); if (id == R.id.nav_item1) { } else if (id == R.id.nav_item2) { } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } }); } }

XML

ChildNavigationActivity(ChildActivity <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".MyAppCompatActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:theme="@style/AppTheme.AppBarOverlay" > <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.DrawerLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/drawer_layout" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:openDrawer="start"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/content" /> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:menu="@menu/menu_navigation_drawer" /> </android.support.v4.widget.DrawerLayout> </android.support.design.widget.CoordinatorLayout>

class

activity_child_navigation(ChildActivity public class ChildNavigationActivity extends MyAppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_child_navigation); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); //we have included "toolbar" in the parentactivity xml. So not need to do it in childactivity xml. /**NOTE: Do Not use "setSupportActionBar(toolbar)" since you have already done it in your parent activity*/ toolbar.setTitle("ChildNavigationActivity"); } }

XML

以上是关于导航菜单在Child Activity中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

导航栏下拉菜单(折叠)在 Bootstrap 5 中不起作用

导航菜单在 Wordpress 的管理面板中不起作用

导航栏中的下拉菜单在 Rails 6 中不起作用

元素导航项下拉菜单在 laravel vue 中不起作用

固定导航菜单在IE 11或Edge中不起作用

jquery 和 bootstrap 导航栏在 Angular 7 中折叠或展开子菜单的布局中不起作用