我已经创建了一个应用程序。它工作的很好,但现在我想在主要活动中包含导航抽屉。我知道代码模板,但为此我需要创建新的活动。我的问题是将抽屉布局包含到仅包含Recycler视图和fab按钮的现有活动中,如果可能包含?对于大问题抱歉。为主要活动添加抽屉布局
回答
你必须把下面的代码在activity_main.xml中
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="com.example.FragmentDrawer"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
我无法将dbactivity扩展到AppCompatActivity。 –
请问您可以发布您的代码 – 2017-03-09 06:29:40
您可以添加NavigationDrawer一步一步按以下程序:
1)设置你的活动父布局DrawerLayout :
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
2)添加NavigationView在d的底部rawerLayout:
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
3)称为activity_main_drawer.xml res文件夹下添加菜单文件夹中的XML文件菜单项:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="@+id/top"
android:checkableBehavior="single">
<item
android:id="@+id/nav_camera"
android:icon="@drawable/ic_menu_camera"
android:title="Home" />
</group>
<group
android:id="@+id/middle"
android:checkableBehavior="single">
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_menu_share"
android:title="Directory" />
</group>
<group
android:id="@+id/bottom"
android:checkableBehavior="single">
<item
android:id="@+id/nav_send"
android:icon="@drawable/ic_menu_send"
android:title="About Us" />
</group>
</menu>
4)在活动实施NavigationView.OnNavigationItemSelectedListener:
public class YourActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
}
5)添加onNavigat ionItemSelected()方法来定义您选择的导航项目上的操作。
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
6)配置DrawerLayout和活动的设置侦听到您NavigationView中的onCreate():
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
7)添加到nav_header_main.xml布局文件夹中;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:src="@android:drawable/sym_def_app_icon" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[email protected]" />
</LinearLayout>
如果您要添加的工具栏,下面的代码粘贴在里面DrawerLayout顶部:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
并关闭CoordinatorLayout的NavigationView前右:
</android.support.design.widget.CoordinatorLayout>
让我知道怎么运行的。
- 1. 打开第二个抽屉布局作为第一个子抽屉布局
- 2. 关闭布局抽屉
- 3. 导航抽屉的主要活动内容
- 4. 如何添加布局到导航抽屉?
- 5. 我可以在导航抽屉布局添加图像头
- 6. 添加一个TextView下面recyclerview抽屉布局
- 7. Android - 抽屉布局 - 关闭抽屉导致空指针异常
- 8. 添加导航抽屉以空白活动
- 9. 如何在导航抽屉活动中添加Google地图?
- 10. Android |将导航抽屉添加到默认地图活动
- 11. 动态添加MDL导航抽屉
- 12. 抽屉布局类未找到错误
- 13. 抽屉布局存在于Xamarin.Android.Support.Core.UI和Xamarin.Android.Support.V4
- 14. Android Studio不会显示抽屉布局
- 15. 带有碎片的Android抽屉布局
- 16. Android Studio的默认Nagivatinal抽屉布局
- 17. Android的抽屉多图布局
- 18. Android材质设计抽屉布局
- 19. 如何将选项卡式活动添加到导航抽屉活动中?
- 20. 使用砌体自动布局的xcode滑动抽屉
- 21. 在导航抽屉布局中显示网格布局?
- 22. 如何在同一功能中打开碎片和活动(带抽屉布局)?
- 23. 如何添加片段主要活动
- 24. 需要动态添加布局
- 25. 在相同的布局中滑动抽屉
- 26. 上面的抽屉布局行动酒吧
- 27. Android:在Eclipse中以图形布局打开滑动抽屉?
- 28. 将滑动抽屉放置在另一个布局顶部
- 29. 材料设计精简版:动态抽屉布局不upgradeElement
- 30. 导航抽屉意向活动Android Studio
你可以扩展你的导航活动到你想使用的活动 – rookieDeveloper
我不能让你的男人sorry.am完全新到android –
@Arun Basker,你试过我的解决方案吗? – tahsinRupam