导航抽屉根本不显示 (android)
Navigation Drawer not showing at all (android)
我遵循了一个相当简单的指南来添加导航抽屉,但是 运行 应用程序时抽屉不存在,并且主屏幕和抽屉的布局都显示在主要活动,重叠。 ://
有什么想法吗?
编辑:属性拼写错误...写了 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);
}
}
这是完全可用的抽屉的布局。注意 android:layout_gravity="start" 属性。如果删除那条线,抽屉将始终打开。这对编辑很有用。
<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>
抽屉活动:
// 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)));
BoilerPlate 方法显示您的抽屉,但是在您的 Activity 中您根本没有调用抽屉
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;
}
});
啊,抱歉各位,问题找到了。只是一个愚蠢的错字。我写了 "android:gravity(...)" insted of "android:layout_gravity(...)" 当我将 Gregorys 代码与我的 ovn 进行比较时实现。谢谢!
我遵循了一个相当简单的指南来添加导航抽屉,但是 运行 应用程序时抽屉不存在,并且主屏幕和抽屉的布局都显示在主要活动,重叠。 ://
有什么想法吗?
编辑:属性拼写错误...写了 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);
}
}
这是完全可用的抽屉的布局。注意 android:layout_gravity="start" 属性。如果删除那条线,抽屉将始终打开。这对编辑很有用。
<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>
抽屉活动:
// 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)));
BoilerPlate 方法显示您的抽屉,但是在您的 Activity 中您根本没有调用抽屉
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;
}
});
啊,抱歉各位,问题找到了。只是一个愚蠢的错字。我写了 "android:gravity(...)" insted of "android:layout_gravity(...)" 当我将 Gregorys 代码与我的 ovn 进行比较时实现。谢谢!