2017-06-12 32 views
0

我正试图实现每个活动上带有工具栏的导航抽屉菜单。我决定创建一个共同的菜单活动类,并且所有其他活动都是从它开始的。它工作正常,除了它将活动放在nav的工具栏下。菜单。无法在导航抽屉菜单的工具栏中获取活动内容

MenuActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 
... 
super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_menu); 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) { 
      /** Called when a drawer has settled in a completely closed state. */ 
      public void onDrawerClosed(View view) { 
       super.onDrawerClosed(view); 
       changeActivity(); 
      } 
     }; 
     drawer.addDrawerListener(toggle); 
     toggle.syncState(); 

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

     navigationView.getMenu().getItem(currentMenuItem).setChecked(true); 
... 

在同一类我管理的项目触摸(菜单项选择):

Intent intent = new Intent(getApplicationContext(), TrainingActivity.class); 
startActivity(intent); 

而且在TrainingActivity:

public class TrainingActivity extends MenuActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     super.setStartingState(MenuItemNames.TRAINING); 
     super.onCreate(savedInstanceState); 
     FrameLayout contentLayout = (FrameLayout) findViewById(R.id.content_frame); 
     View contentView = getLayoutInflater().inflate(R.layout.activity_training, contentLayout, false); 
     drawer.addView(contentView, 0); 

//  setContentView(R.layout.activity_training); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

...

这里是activity_menu .xml:

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 
     <include 
      layout="@layout/app_bar_menu" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/content_frame"/> 
    </LinearLayout> 

    <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/activity_menu_drawer" /> 

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

的app_bar_menu.xml:

<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:id="@+id/app_bar_menu_coord_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.magnifi.pennantrace.MenuActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     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.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/relativelayout" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:textStyle="bold" 
        android:layout_height="wrap_content" 
        android:id="@+id/toolbar_div_rank" 
        android:text="4th Place" 
        app:layout_constraintTop_toTopOf="parent" 
        app:layout_constraintLeft_toLeftOf="parent" /> 

       ... 

      </android.support.constraint.ConstraintLayout> 
     </android.support.v7.widget.Toolbar> 

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

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

所以我想执行的逻辑是,只要项目在菜单中选择,我加载(充气它)适当活动到的FrameLayout(content_frame下activity_menu.xml中的工具栏)。所以好像它必须在工具栏下,但它不是。你能帮我一下,告诉我我的错误在哪里?

+0

'drawer.addView(内容查看,0);' - 你要添加'contentView'到'contentLayout',不'drawer' –

+0

@ MikeM。如果我做contentLayout.addView(contentView,0);那么除了菜单/工具栏外,它不会显示任何内容。 – maximus

+0

是的,刚才注意到,在垂直'LinearLayout'里面有两个东西,它们持有'content_frame',并且都有'match_parent'高度,所以'content_frame'被推出底部。您可能希望将'content_frame'移动到'app_bar_menu'中,并在'DrawerLayout'中摆脱封闭的'LinearLayout'。 –

回答

0

我已经通过替换片段的活动解决了问题。的 因此,而不是宣布活动: public class TrainingActivity extends MenuActivity {

我取代它由:活动 public class TrainingActivity extends Fragment {

例改为片段:

public class TrainingActivity extends Fragment { 

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

    public static TrainingActivity newInstance(String param1, String param2) { 
     TrainingActivity fragment = new TrainingActivity(); 
     Bundle args = new Bundle(); 
     fragment.setArguments(args); 
     return fragment; 
    } 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.activity_training, container, false); 
     imageButtonsSetup(view); 
     return view; 
    } 

    private void imageButtonsSetup(View view) { 
     setImageButtonWithResourceId(view, R.id.imageButtonSelectTraining); 
     setImageButtonWithResourceId(view, R.id.imageButtonSelectPlayers); 
    } 

    private void setImageButtonWithResourceId(View view, int resourceId) { 
     ((ImageButton)view.findViewById(resourceId)).setOnTouchListener(new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       switch (event.getAction()) { 
        case MotionEvent.ACTION_DOWN: { 
         ImageButton view = (ImageButton) v; 
         view.getBackground().setColorFilter(0x77777700, PorterDuff.Mode.SRC_ATOP); 
         v.invalidate(); 
         break; 
        } 
        case MotionEvent.ACTION_UP: 

         // Your action here on button click 

        case MotionEvent.ACTION_CANCEL: { 
         ImageButton view = (ImageButton) v; 
         view.getBackground().clearColorFilter(); 
         view.invalidate(); 
         break; 
        } 
       } 
       return true; 
      } 
     }); 
    } 
} 

如果所选的菜单项改变我做了以下:

try { 
       fragment = (Fragment) fragmentClass.newInstance(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      FragmentManager fragmentManager = getSupportFragmentManager(); 
      fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); 

所以我没有'不再需要抽屉addView等。因为我现在正在替换content_frame。

最终app_bar_menu.xml改变这样的:

<LinearLayout 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:id="@+id/app_bar_menu_coord_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:fitsSystemWindows="true" 
    tools:context="com...."> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     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.v7.widget.Toolbar> 
    </android.support.design.widget.AppBarLayout> 

    <FrameLayout 
     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" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:id="@+id/content_frame"/> 
</LinearLayout>