lem*_*anh 51 navigation android drawer android-actionbar
我正试图将导航抽屉放在操作栏上,当它向右滑动时就像这个应用程序:[已删除]
这是我的主要活动的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout ...>
<RelativeLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
...
</RelativeLayout>
<fragment android:name="com...."
android:layout_gravity="start"
android:id="@id/navigation"
android:layout_width="@dimen/navigation_menu_width"
android:layout_height="fill_parent" />
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
stackoverflow上的其他一些问题与此问题类似, 但所有答案都建议使用滑动菜单库.但是这个应用程序他们仍然使用android.support.v4.widget.DrawerLayout并且他们成功了.不要问我怎么知道他们使用标准导航抽屉,但我确定.
非常感谢您的帮助.
这是最终的解决方案:非常感谢@Peter Cai这项工作完美无缺. https://github.com/lemycanh/DrawerOnTopActionBar
Pet*_*Cai 51
我从https://github.com/jfeinstein10/SlidingMenu学到了一个小技巧,以实现您所需的效果.
您只需要删除窗口装饰视图的第一个子项,并将第一个子项添加到抽屉的内容视图中.之后,您只需将抽屉添加到窗口的装饰视图中.
以下是您执行此操作的一些详细步骤.
首先,创建一个名为"decor.xml"的xml或任何你喜欢的东西.只放入DrawerLayout和抽屉.下面的"FrameLayout"只是一个容器.我们将用它来包装您活动的内容.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout ...>
<FrameLayout android:id="@+id/container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<fragment android:name="com...."
android:layout_gravity="start"
android:id="@id/navigation"
android:layout_width="@dimen/navigation_menu_width"
android:layout_height="fill_parent" />
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
然后删除主布局中的DrawerLayout.现在主要活动的布局应该是这样的
<RelativeLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
...
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我们假设主要活动的布局名为"main.xml".
在您的MainActivity中,写如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Inflate the "decor.xml"
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
DrawerLayout drawer = (DrawerLayout) inflater.inflate(R.layout.decor, null); // "null" is important.
// HACK: "steal" the first child of decor view
ViewGroup decor = (ViewGroup) getWindow().getDecorView();
View child = decor.getChildAt(0);
decor.removeView(child);
FrameLayout container = (FrameLayout) drawer.findViewById(R.id.container); // This is the container we defined just now.
container.addView(child);
// Make the drawer replace the first child
decor.addView(drawer);
// Do what you want to do.......
}
Run Code Online (Sandbox Code Playgroud)
现在你有一个可以在ActionBar上滑动的DrawerLayout.但您可能会发现状态栏覆盖了它.您可能需要向Drawer添加paddingTop才能解决问题.
Ric*_*out 34
更新:如何使用导航抽屉覆盖操作栏.(使用新工具栏)在build.gradle中的依赖项中使用这些
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.android.support:support-v4:21.0.0'
Run Code Online (Sandbox Code Playgroud)
这是您的抽屉布局
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<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">
<LinearLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar"/>
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"/>
</LinearLayout>
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/list_background"
/>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
在布局文件夹中创建新的toolbar.xml文件.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
Run Code Online (Sandbox Code Playgroud)
转到扩展导航抽屉的活动.并在SetContentView()之后添加它
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Run Code Online (Sandbox Code Playgroud)
不要忘记在您的values文件夹中扩展主题NoActionBar.
<style name="Theme.Whtsnxt" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<!-- colorPrimary is used for the default action bar background -->
<item name="windowActionModeOverlay">true</item>
<item name="android:textColorPrimary">@color/white</item>
<item name="colorPrimary">@color/splashscreen</item>
<item name="colorPrimaryDark">@color/holo_blue_light</item>
<item name="android:windowBackground">@color/white</item>
<item name="android:colorBackground">@color/white</item>
</style>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
64624 次 |
最近记录: |