简体   繁体   English

导航抽屉未显示

[英]Navigation Drawer not showing up

So I have an old application. 所以我有一个旧的应用程序。 I wanted to add it a navigation drawer. 我想添加一个导航抽屉。 So I want the navigation drawer to work with my current activity and to show ONLY on the currecnt activity 因此,我希望导航抽屉与我当前的活动配合使用,并且仅在当前活动上显示

public class WebViewActivity extends Activity {

So the main activity is a webView. 因此,主要活动是一个webView。

<?xml version="1.0" encoding="utf-8"?>
<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">


<!--
      main content
 -->

<RelativeLayout

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#ffffff" >

    <WebView

        android:id="@+id/webView1"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:background="#ffffff" />

</RelativeLayout>

<!--
    navigation list item
-->
<FrameLayout

    android:id="@+id/content_frame"

    android:layout_width="match_parent"

    android:layout_height="match_parent" />

<ListView

    android:id="@+id/left_drawer"

    android:layout_width="240dp"

    android:layout_height="match_parent"

    android:layout_gravity="left"

    android:background="#2A323D"

    android:cacheColorHint="#00000000"

    android:choiceMode="singleChoice" />

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

So I expect my drawer to appear when I click the menu button or my webView detect the gesture left to right 因此,我希望当我单击菜单按钮或WebView从左到右检测到手势时,抽屉会出现

    mTitle = mDrawerTitle = getTitle();
    mPlanetTitles = getResources().getStringArray(R.array.planets_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    drawerView = (View)findViewById(R.id.left_drawer);

    mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
            R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description for accessibility */
            R.string.drawer_close  /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mPlanetTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

AND

        webView.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    x1 = event.getX();
                    break;
                case MotionEvent.ACTION_UP:
                    x2 = event.getX();
                    float deltaX = x2 - x1;
                    if (Math.abs(deltaX) > MIN_DISTANCE) {
                        mDrawerLayout.openDrawer(GravityCompat.START);
                        return false;
                    } else {
                        // consider as something else - a screen tap for example
                    }
                    break;
            }
            return true;
        }
    });

I really don't know what am I missing, but mDrawerLayout.openDrawer(GravityCompat.START); 我真的不知道我缺少什么,但是mDrawerLayout.openDrawer(GravityCompat.START); is not doing anything. 什么也没做 There are no errors in the log. 日志中没有错误。 The suppossed drawer does not show up, nothing happens. 假定的抽屉没有出现,什么也没发生。 I followed the official guide and more online. 我遵循了官方指南以及更多在线内容。 No luck. 没运气。

I don't want to use Fragments! 我不想使用碎片!

UPDATE 更新

The issue is that in my 'onCreate' I have something like: setContentView(webView); 问题是,在我的“ onCreate”中,我有类似以下内容: setContentView(webView);

So if I remove it, my navigation drawer works! 因此,如果我删除它,我的导航抽屉将起作用! BUT my webView does not. 但是我的webView没有。 What am I missing? 我想念什么?

从main_activity中删除FrameLayout,然后尝试

<?xml version="1.0" encoding="utf-8"?>
<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">

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >

<WebView
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff" />

</RelativeLayout>

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#2A323D"
    android:cacheColorHint="#00000000"
    android:choiceMode="singleChoice" />

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

Just copy and paste it 只需复制并粘贴

Your onCreate() must have setContentView(R.layout.drawer_layout); 您的onCreate()必须具有setContentView(R.layout.drawer_layout); where drawer_layout is the xml which has a DrawerLayout as its root view 其中抽屉式布局是具有DrawerLayout作为其根视图的xml

Follow the documentation Download sample code and see which xml is passed to setContentView() 按照文档 下载示例代码,查看将哪个xml传递给setContentView()

If you follow it carefully you will notice activity_main.xml contains Drawerlayout and not the main content's xml. 如果仔细遵循,您会注意到activity_main.xml包含Drawerlayout而不是主要内容的xml。

Also keep in mind that DrawerLayout must contain exactly two child ViewGroup s, where the first can be a fragment or FrameLayout that populates a fragment at runtime AND the second must be NavigationView 还要记住, DrawerLayout必须恰好包含两个子ViewGroup ,其中第一个可以是片段或在运行时填充一个片段的FrameLayout,而第二个必须是NavigationView

If you are still confused you can follow this guide which has step-by-step instructions. 如果您仍然感到困惑,可以按照本指南进行操作 ,其中包含分步说明。 Its worth a visit. 它值得一游。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM