【发布时间】:2014-04-16 08:44:52
【问题描述】:
嗨,我正在做一个 android 项目,我想在所有页面中显示导航抽屉。在主屏幕中,想要在左侧顶部显示导航抽屉,在屏幕右上角的圆圈内显示登录该应用的用户的图片。
如果有人知道使用导航抽屉创建自定义操作栏,请帮助我。
【问题讨论】:
标签: android android-actionbar navigation-drawer
嗨,我正在做一个 android 项目,我想在所有页面中显示导航抽屉。在主屏幕中,想要在左侧顶部显示导航抽屉,在屏幕右上角的圆圈内显示登录该应用的用户的图片。
如果有人知道使用导航抽屉创建自定义操作栏,请帮助我。
【问题讨论】:
标签: android android-actionbar navigation-drawer
您需要在菜单文件夹中为右侧的图像添加一个菜单文件。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/your_id"
android:icon="@drawable/the image you want"
android:orderInCategory="100"
android:showAsAction="always"
android:title="Your title"/>
</menu>
然后在你想把这段代码放进去的activity中(onCreate之外):
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
这会将菜单放在顶部。
现在对于右侧的图像(三行),将其放入活动的 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_layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FirstActivity" >
<!-- The main content goes here -->
<!-- The navigation drawer -->
<ListView
android:id="@+id/drawer1"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FFFFFF"
android:choiceMode="singleChoice"
android:divider="#FF0000"
android:dividerHeight="1dp"
android:paddingLeft="15sp"
android:paddingRight="15sp" />
</android.support.v4.widget.DrawerLayout>
然后把这部分放到你的 java.lang.您需要从某个地方获取 ic-drawer 图像,但到处都是,所以只需 google 即可。
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout1);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
drawer, /* DrawerLayout object */
R.drawable.ic_drawer, /* This is the image for top left*/
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
);
【讨论】: