看不到导航抽屉 [英] Unable to see navigation drawer

查看:91
本文介绍了看不到导航抽屉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里经历了类似的问题及其解决方案,但是花了几个小时才解决我的问题.我查看了许多解决方案,并尝试了它们,但仍然无法在屏幕上看到导航抽屉.如果有人能告诉我我在这里想念什么,我将不胜感激.

I have gone through similar questions and their solutions here, but I can't get past my issue after spending few hours on it. I looked at many solutions, tried them, but still can't get to see the navigation drawer on my screen. I will really appreciate if someone could tell me what am I missing here.

提前谢谢.

这是我的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:

Here's my 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));
>    }
>}

我没有看到任何输出.屏幕输出

I don't see any output.Screen output

推荐答案

<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

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>

这篇关于看不到导航抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆