导航抽屉根本没有显示(android)

时间:2016-07-09 23:46:06

标签: android layout navigation navigation-drawer overlapping

我按照一个相当简单的指南来添加导航抽屉,但是在运行应用程序时抽屉不在那里,并且主屏幕和抽屉的布局都显示在主要活动中,重叠。 ://

有什么想法吗?

编辑:错误拼写了一个属性...写了android:gravity,而不是android:layout_gravity 感到愚蠢:P

这是xml布局:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.kapres.test2.MainActivity">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/main_screen"
            android:scaleType="centerCrop"/>

        <TextView
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test2"
            android:textSize="38dp"
            android:paddingTop="32dp"/>

    </RelativeLayout>

    <ListView
        android:id="@+id/main_menu_layout"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:gravity="left|start">

    </ListView>

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

这里是java代码:

package com.kapres.test2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

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

        ArrayList<String> mainMenuList = new ArrayList<String>();

        mainMenuList.add("item1");
        mainMenuList.add("item2");
        mainMenuList.add("item3");
        mainMenuList.add("item4");
        mainMenuList.add("item5");

        ArrayAdapter mainMenuAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, mainMenuList);
        ListView listView = (ListView) findViewById(R.id.main_menu_layout);
        listView.setAdapter(mainMenuAdapter);
    }
}

3 个答案:

答案 0 :(得分:0)

这里是完全工作抽屉的布局。注意android:layout_gravity =&#34; start&#34;属性。如果删除该行,则始终打开抽屉。这对编辑很有用。

<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 -->

<LinearLayout
    android:id="@+id/drawer_content"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="start"
    android:background="@color/background">

</LinearLayout>

DrawerActivity:

// 1. Set content view
    setContentView(R.layout.app_drawer);
    // 2. Set drawer layout
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {

        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // Update user data end etc.
            loadUserDataOntoDrawer();
        }

        @Override
        public void onDrawerClosed(View drawerView) {

        }

        @Override
        public void onDrawerStateChanged(int newState) {

        }
    });
    mDrawerContent = (LinearLayout) findViewById(R.id.drawer_content);

    // Initialize your data adapter
    mMenuDataAdapter = new MenuDataAdapter(MenuHolder.getInstance().getMenuItemList());
    // Find recyclerview
    mRecyclerView = (RecyclerView) findViewById(R.id.left_drawer);
    // Define layout manager
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    // Set adapter
    mRecyclerView.setAdapter(mMenuDataAdapter);
    // Set animator
    mRecyclerView.setItemAnimator(new SlideInUpAnimator(new OvershootInterpolator(1f)));

答案 1 :(得分:0)

BoilerPlate方法显示你的抽屉然而在你的活动中你根本没有打电话给抽屉

  private NavigationView navigationView;
  private DrawerLayout drawerLayout;

然后

  private void setupDrawerLayout() {
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        navigationView = (NavigationView) findViewById(R.id.navigation_view);

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                Toast.makeText(getApplicationContext(), menuItem.getItemId()
                + menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();

                menuItem.setChecked(true);
                drawerLayout.closeDrawers();
                return true;
            }
        });

答案 2 :(得分:0)

啊,对不起伙计们,发现了问题。只是一个愚蠢的错字。我写了#34; android:gravity(...)&#34; &#34; android:layout_gravity(...)&#34;当我将Gregorys代码与我的ovn进行比较时实现了。谢谢!