2016-02-09 49 views
0

我一直在休息一個touch + tutorials android beginners從一個星期,我在我的main.xml文件中使用android.support.v4.widget.Drawer佈局(抽屜佈局)。但它不工作,我不能在工具欄上看到抽屜。android.support.v4.widget.DrawerLayout不工作

錯誤我感到getiing低於

02-09 08:30:18.730 24972-24972/? D/dalvikvm: Late-enabling CheckJNI 
02-09 08:30:18.742 24972-24972/? W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 
02-09 08:30:18.742 24972-24972/? I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested 
02-09 08:30:18.742 24972-24972/? W/dalvikvm: VFY: unable to resolve interface method 17962: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 
02-09 08:30:18.746 24972-24972/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
02-09 08:30:18.746 24972-24972/? I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode 
02-09 08:30:18.746 24972-24972/? W/dalvikvm: VFY: unable to resolve interface method 17966: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 
02-09 08:30:18.746 24972-24972/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
02-09 08:30:18.754 24972-24978/? D/dalvikvm: Debugger has detached; object registry had 1 entries 
02-09 08:30:18.758 24972-24972/? I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations 
02-09 08:30:18.758 24972-24972/? W/dalvikvm: VFY: unable to resolve virtual method 421: Landroid/content/res/TypedArray;.getChangingConfigurations()I 
02-09 08:30:18.758 24972-24972/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
02-09 08:30:18.758 24972-24972/? I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType 
02-09 08:30:18.758 24972-24972/? W/dalvikvm: VFY: unable to resolve virtual method 443: Landroid/content/res/TypedArray;.getType (I)I 
02-09 08:30:18.758 24972-24972/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
02-09 08:30:18.814 24972-24972/? D/dalvikvm: GC_FOR_ALLOC freed 134K, 11% free 2451K/2728K, paused 1ms, total 2ms 
02-09 08:30:18.826 24972-24972/? W/ActionBarDrawerToggle: DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true); 
02-09 08:30:18.838 24972-24972/? D/libEGL: loaded /system/lib/egl/libEGL_genymotion.so 
02-09 08:30:18.842 24972-24972/? D/libEGL: loaded /system/lib/egl/libGLESv1_CM_genymotion.so 
02-09 08:30:18.842 24972-24972/? D/libEGL: loaded /system/lib/egl/libGLESv2_genymotion.so 
02-09 08:30:18.874 24972-24972/? W/EGL_genymotion: eglSurfaceAttrib not implemented 
02-09 08:30:18.886 24972-24972/? D/OpenGLRenderer: Enabling debug mode 0 
02-09 08:30:18.890 24972-24975/? D/dalvikvm: GC_CONCURRENT freed 45K, 7% free 2794K/3004K, paused 2ms+0ms, total 3ms 
02-09 08:53:28.198 24972-24975/com.example.lucy.zoo D/dalvikvm: GC_CONCURRENT freed 379K, 16% free 2799K/3320K, paused 0ms+0ms, total 4ms 

的代碼是好的,但我不知道是什麼的問題。任何人都可以看看我的代碼,並告訴我問題在哪裏。提前致謝。

public class MainActivity extends AppCompatActivity { 
private DrawerLayout mdrawerlyout; 
    private ActionBarDrawerToggle mActionBarDrawerToggle; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 

     mdrawerlyout = (DrawerLayout)findViewById(R.id.drawer_layout); 
     mActionBarDrawerToggle = new ActionBarDrawerToggle(this,mdrawerlyout,R.string.drawer_opend,R.string.drawer_closed){ 
      @Override 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       if(getSupportActionBar() != null) { 


        getSupportActionBar().setTitle(R.string.drawer_opend); 
       } 
      } 

      @Override 
      public void onDrawerClosed(View drawerView) { 
       super.onDrawerClosed(drawerView); 
       if(getSupportActionBar() !=null) { 
        getSupportActionBar().setTitle(R.string.drawer_closed); 
       } 
      } 
     }; 
     mdrawerlyout.setDrawerListener(mActionBarDrawerToggle); 

    } 

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

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     mActionBarDrawerToggle.onConfigurationChanged(newConfig); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.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. 

     if(mActionBarDrawerToggle.onOptionsItemSelected(item)) 
     { 
      return true; 
     } 
     int id = item.getItemId(); 


     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

下面是我的佈局文件

<?xml version="1.0" encoding="utf-8"?> 
<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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="com.example.lucy.zoo.MainActivity" 
    tools:showIn="@layout/activity_main"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:background="?attr/colorPrimary" 
     android:minHeight="?android:attr/actionBarSize" /> 

    <android.support.v4.widget.DrawerLayout 
     android:id="@+id/drawer_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello World!" /> 
    <ListView 
     android:id="@+id/drawer" 
     android:layout_gravity="start" 
     android:layout_width="240dp" 
     android:layout_height="match_parent"></ListView> 
</android.support.v4.widget.DrawerLayout> 
</LinearLayout> 
+0

http://stackoverflow.com/questions/28105400/navigation-drawer-icon-not-showing-android –

+0

IIRC,DrawerLayout應該是根元素 – Kezufru

+0

集'DrawerLayout'父/ root而不是'LinearLayout' –

回答

0

首先,你必須填寫列表視圖項比你要分配列表視圖抽屜佈局。

看到這個從Android Studio中創建新的項目 =>選擇添加活動抽屜式導航活動,移動窗口

0
<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/drawerrrrFolder" 
    > 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 




     //your whole layout comes here 

    </RelativeLayout> 




         <!--Drawer LAyout--> 
    <RelativeLayout 
     android:id="@+id/mydrawerFolder" 
     android:layout_width="240dp" 
     android:layout_height="fill_parent" 
     android:background="#ffffff" 
     android:layout_gravity="start"> 

        <RelativeLayout 
         android:id="@+id/sslayoutmydrawerFolder" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content"> 

           <RelativeLayout 
            android:id="@+id/searcboxxxmydrawerFolder" 
            android:layout_width="wrap_content" 
            android:layout_height="50dp" 
             android:layout_marginTop="10dp" 

            > 
            <EditText 
             android:id="@+id/editsearchtextmydrawerFolder" 
             android:layout_width="fill_parent" 
             android:layout_height="50dp" 
             android:background="@drawable/drawwersearchback" 
             android:layout_marginLeft="20dp" 
             android:layout_centerVertical="true" 
             android:layout_alignParentLeft="true" 
             android:layout_marginTop="10dp" 
             android:layout_toLeftOf="@+id/cancelsearchboxlayoutmydrawerFolder" 
             android:paddingLeft="50dp"/> 



            <RelativeLayout 
             android:id="@+id/cancelsearchboxlayoutmydrawerFolder" 
             android:layout_width="60dp" 
             android:layout_height="50dp" 
             android:layout_alignParentRight="true" 
             > 
             <ImageView 
              android:layout_width="30dp" 
              android:layout_height="30dp" 
              android:background="@drawable/searchcancelicon" 
              android:layout_centerInParent="true" 

              /> 

            </RelativeLayout> 


            <RelativeLayout 
             android:layout_width="50dp" 
             android:layout_height="50dp" 

             android:layout_marginLeft="15dp" 

             > 
             <ImageView 
              android:layout_width="20dp" 
              android:layout_height="20dp" 
              android:background="@drawable/drawersearchicon" 
              android:layout_centerInParent="true" 
              /> 

            </RelativeLayout> 

           </RelativeLayout> 








     </RelativeLayout> 
     <ListView 
      android:id="@+id/drawerlistitemsmydrawerFolder" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_below="@+id/sslayoutmydrawerFolder" 
      android:divider="@null" 
      android:scrollbars="none" 
      > 

     </ListView> 
    </RelativeLayout> 


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

希望你有這個。

1

這是你應該如何實現它。

public class NavigationDrawerFragment extends Fragment { 

    private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position"; 
    private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned"; 
    private NavigationDrawerCallbacks mCallbacks; 
    private ActionBarDrawerToggle mDrawerToggle; 
    private DrawerLayout mDrawerLayout; 
    private ListView mDrawerListView; 
    private View mFragmentContainerView; 
    private int mCurrentSelectedPosition = 0; 
    private boolean mFromSavedInstanceState; 
    private boolean mUserLearnedDrawer; 

    public NavigationDrawerFragment() { 
    } 

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

     SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
     mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false); 

     if (savedInstanceState != null) { 
      mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION); 
      mFromSavedInstanceState = true; 
     } 

     selectItem(mCurrentSelectedPosition); 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     setHasOptionsMenu(true); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     mDrawerListView = (ListView) inflater.inflate(
       R.layout.fragment_navigation_drawer, container, false); 
     mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       selectItem(position); 
      } 
     }); 
     mDrawerListView.setAdapter(new ArrayAdapter<String>(
       getActionBar().getThemedContext(), 
       android.R.layout.simple_list_item_1, 
       android.R.id.text1, 
       new String[]{ 
         getString(R.string.title_section1), 
         getString(R.string.title_section2), 
         getString(R.string.title_section3), 
       })); 
     mDrawerListView.setItemChecked(mCurrentSelectedPosition, true); 
     return mDrawerListView; 
    } 

    public boolean isDrawerOpen() { 
     return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView); 
    } 

    public void setUp(int fragmentId, DrawerLayout drawerLayout) { 
     mFragmentContainerView = getActivity().findViewById(fragmentId); 
     mDrawerLayout = drawerLayout; 

     mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 

     ActionBar actionBar = getActionBar(); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     actionBar.setHomeButtonEnabled(true); 

     mDrawerToggle = new ActionBarDrawerToggle(
       getActivity(),      
       mDrawerLayout,      
       R.drawable.ic_drawer,    
       R.string.navigation_drawer_open, 
       R.string.navigation_drawer_close 
     ) { 
      @Override 
      public void onDrawerClosed(View drawerView) { 
       super.onDrawerClosed(drawerView); 
       if (!isAdded()) { 
        return; 
       } 

       getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu() 
      } 

      @Override 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       if (!isAdded()) { 
        return; 
       } 

       if (!mUserLearnedDrawer) { 
        mUserLearnedDrawer = true; 
        SharedPreferences sp = PreferenceManager 
          .getDefaultSharedPreferences(getActivity()); 
        sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).commit(); 
       } 

       getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu() 
      } 
     }; 

     if (!mUserLearnedDrawer && !mFromSavedInstanceState) { 
      mDrawerLayout.openDrawer(mFragmentContainerView); 
     } 

     mDrawerLayout.post(new Runnable() { 
      @Override 
      public void run() { 
       mDrawerToggle.syncState(); 
      } 
     }); 

     mDrawerLayout.setDrawerListener(mDrawerToggle); 
    } 

    private void selectItem(int position) { 
     mCurrentSelectedPosition = position; 
     if (mDrawerListView != null) { 
      mDrawerListView.setItemChecked(position, true); 
     } 
     if (mDrawerLayout != null) { 
      mDrawerLayout.closeDrawer(mFragmentContainerView); 
     } 
     if (mCallbacks != null) { 
      mCallbacks.onNavigationDrawerItemSelected(position); 
     } 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     try { 
      mCallbacks = (NavigationDrawerCallbacks) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException("Activity must implement NavigationDrawerCallbacks."); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mCallbacks = null; 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     mDrawerToggle.onConfigurationChanged(newConfig); 
    } 

    private void showGlobalContextActionBar() { 
     ActionBar actionBar = getActionBar(); 
     actionBar.setDisplayShowTitleEnabled(true); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
     actionBar.setTitle(R.string.app_name); 
    } 

    private ActionBar getActionBar() { 
     return ((ActionBarActivity) getActivity()).getSupportActionBar(); 
    } 

    public static interface NavigationDrawerCallbacks { 
     void onNavigationDrawerItemSelected(int position); 
    } 
} 

我已經使用following tutorial來幫助我實現它。希望這將有助於你太:)

相關問題