2016-07-25 100 views
0

我已经在这里经历了类似的问题和他们的解决方案,但花了几个小时后,我无法解决我的问题。我查看了许多解决方案,尝试了它们,但仍无法看到屏幕上的导航抽屉。如果有人能告诉我我在这里错过了什么,我将非常感激。无法看到导航抽屉

在此先感谢。

这里是我的xml:

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

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

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      tools:context=".Menu"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center_vertical" 
       android:paddingLeft="16dp" 
       android:paddingRight="16dp" 
       android:textColor="#fff" 
       android:text="test" 
       android:id="@+id/text" /> 

     </RelativeLayout> 

     <ListView 
      android:layout_width="200dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="end" 
      android:choiceMode="singleChoice" 
      android:background="#fff" 
      android:id="@+id/left_drawer" > 
     </ListView> 

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

这里是我的Menu.java:

>public class Menu extends AppCompatActivity { 
> 
> public DrawerLayout dlayout; 
> public ListView flist; 
> public String[] menu; 
> //public ArrayAdapter<String> mAdapter; 
> 
> @Override 
> protected void onCreate(Bundle savedInstanceState) { 
>  super.onCreate(savedInstanceState); 
>  setContentView(R.layout.activity_menu); 
> 
>  menu = getResources().getStringArray(R.array.nav_drawer_items); 
>  dlayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
>  flist = (ListView) findViewById(R.id.left_drawer); 
> 
>  flist.setAdapter(new ArrayAdapter<String>(this, R.layout.activity_menu, >R.id.text, menu)); 
> } 
>} 

我没有看到任何输出。 Screen output

+0

你必须建立在最上面的左下角菜单图标。你必须为图标编码,当你点击该图标时,抽屉会打开/关闭。 –

+0

谢谢达里尔。我正在执行您的建议。 – DK15

回答

0
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<include 
    android:id="@+id/header" 
    layout="@layout/menu"/> 

<FrameLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:id="@+id/frame" 
     android:textSize="24sp" 
     android:gravity="center" 
     android:layout_marginTop="50dp"/> 

<ListView 
    android:id="@+id/list_item" 
    android:layout_width="200dp" 
    android:layout_height="match_parent" 
    android:dividerHeight="1dp" 
    android:layout_gravity="left|start" 
    android:background="#ffeeeeee"/></android.support.v4.widget.DrawerLayout> 

MainActivity。java的

public class MainActivity extends Activity { 

String[] names = {"android","java","spring","html"}; 
ListView list; 
FrameLayout frame; 
ActionBarDrawerToggle action; 
DrawerLayout drawer; 
Button but; 


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

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    frame = (FrameLayout) findViewById(R.id.frame); 
    list = (ListView) findViewById(R.id.list_item); 
    but = (Button) findViewById(R.id.menu); 

    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, names); 
    list.setAdapter(adapter); 

    action = new ActionBarDrawerToggle(this, drawer, R.string.drawer_open, R.string.drawer_close) { 

     /* Called when drawer is closed */ 
     public void onDrawerClosed(View view) { 
      //Put your code here 
     } 

     /* Called when a drawer is opened */ 
     public void onDrawerOpened(View drawerView) { 
      //Put your code here 
     } 
    }; 
    drawer.setDrawerListener(action); 

    but.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (drawer != null) { 
       if (drawer.isDrawerOpen(list)) { 
        drawer.closeDrawer(list); 
       } else { 
        drawer.openDrawer(list); 
        process(); 
       } 
      } 
     } 
    }); 


} 

private void process() { 

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      FragmentTransaction ft=getFragmentManager().beginTransaction(); 

      switch (names[position]) 
      { 
       case "android": 
       { 
        yourClass obj=new yourClass(); 
        //ft.replace(R.id.frame,obj); 
        break 
       } 
       case "java": 
       { 
        yourClass obj=new yourClass(); 
        //ft.replace(R.id.frame,obj); 
        break; 
       } 
       case "spring": 
       { 
        yourClass obj=new yourClass(); 
        //ft.replace(R.id.frame,obj); 
        break; 
       } 
       case "html": 
       { 
        yourClass obj=new yourClass(); 
        //ft.replace(R.id.frame,obj); 
        break; 
       } 

       default: 
       { 
        Toast.makeText(MainActivity.this,"you have to click another one",Toast.LENGTH_LONG).show(); 
       } 
      } 
      ft.commitAllowingStateLoss(); 
      list.setItemChecked(position, true); 
      drawer.closeDrawer(list); 
     } 

    }); 
} 

}

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:layout_width="match_parent" 
tools:context=".MainActivity" 
android:layout_height="wrap_content"> 

<Button 
    android:id="@+id/menu" 
    android:title="menu" 
    android:icon="@drawable/menu" 
    android:background="@drawable/menu" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:backgroundTint="#0a0a0a" /></LinearLayout> 
0

您需要切换抽屉的打开/关闭。 要做到这一点,你可以做到以下几点:

1 - 实现toggleDrawer方法

public void toggleDrawer(boolean shouldBeVisible){ 
    if(shouldBeVisible){ 
     dlayout.openDrawer(dlayout); 
    } else { 
     dlayout.closeDrawers(); 
    } 
} 

2 - 调用它的onCreate(注:这只是用于测试)

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

3 - 就像Daryl说的那样,实现一个按钮,这样你就可以主动地切换(打开和关闭抽屉到y我们喜欢)

myButton = (Button) findViewById(R.id.myButton); 
myButton.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v){ 
     if(dlayout.getVisiblity() == View.VISIBLE) { 
      // drawer is visible -> close 
      toggleDrawer(false); 
     } else { 
      // drawer is gone/invisible -> open 
      toggleDrawer(true); 
     } 
    } 
}) 

4 - (请确保您的DrawerLayout只包含2子视图)

要添加抽屉式导航,带有 DrawerLayout对象作为根视图声明你的用户界面的布局。在 DrawerLayout中,添加一个视图,其中包含 屏幕(抽屉被隐藏时的主要布局)和包含抽屉导航内容的另一个 的主要内容。

注:忘了补充这一点,但确实是一个非常宝贵的参考(感谢@Meet,upvoted)

2

似乎应该有你的布局文件的问题。

记住DrawerLayout允许最大 孩子的意见。有 Google文档this

应该是这样:

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <!-- The main content view --> 
    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
    <!-- The navigation drawer --> 
    <ListView android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:background="#111"/> 
</android.support.v4.widget.DrawerLayout> 
+0

谢谢@Meet。这有帮助。 – DK15