我成功创建了一个ListView,并使用AndroidStudio提供的示例将其显示为滑动菜单,以创建滑动菜单
现在我想在ListView上面添加一个布局(显示已登录用户的名称和图像,但为了简化我只想放一个TextView)。
我尝试了以下操作,但没有成功:
layout_menu.xml(当我想显示菜单时出现的布局。这被修改为放置textview)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NavigationDrawerFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User logged in"/>
<ListView
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="@color/fondo_menu"
android:id="@+id/listaMenu"
tools:context=".NavigationDrawerFragment" />
</LinearLayout>
activity_base.xml(显示菜单的活动使用的那个,未被修改)
<!-- 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"
tools:context=".BaseActivity">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container">
</ViewFlipper>
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.proyecto.cutcsa.cutcsa.Interfaz.ElementosUI.NavigationDrawerFragment"
tools:layout="@layout/layout_menu"/><!--tools:layout="@layout/fragment_navigation_drawer" -->
</android.support.v4.widget.DrawerLayout>
NavigationDrawerFragment类(仅修改onCreateView以显示新菜单)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/*start modified code*/
View view = inflater.inflate(R.layout.layout_menu, container, false);
mDrawerListView = (ListView) view.findViewById(R.id.listaMenu);
/*finish modified code*/
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
}
但我得到java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
onCreate()上会出现此错误,尤其是我要显示菜单的活动中的setContentView(R.layout.activity_base)
行。
我定义的onCreate方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
Bundle bundle = getIntent().getExtras();
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
try{
mTitle = bundle.getString("title_menu");
}catch(Exception ex){}
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
}
我做错了什么?
答案 0 :(得分:0)
您尝试将片段充气2次,此处:
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.proyecto.cutcsa.cutcsa.Interfaz.ElementosUI.NavigationDrawerFragment"
tools:layout="@layout/layout_menu"/><!--tools:layout="@layout/fragment_navigation_drawer" -->
您只需将此布局(导航抽屉内容)添加到您的活动中:
<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">
<!-- USE VIEWPAGER INSTEAD -->
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container">
</ViewFlipper>
<LinearLayout
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User logged in"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="@color/fondo_menu"
android:id="@+id/listaMenu"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
答案 1 :(得分:0)
使用Android Design Support Library
大大简化了此模式文章说要像这样创建你的抽屉:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/drawer"
/>
</android.support.v4.widget.DrawerLayout>
headerLayout指的是位于列表上方的布局(您的帐户详细信息布局)。菜单是标准菜单资源,其中一个例子是:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="@+id/nav_group_fragments"
android:checkableBehavior="single">
<item
android:id="@+id/nav_home_one"
android:checked="true"
android:title="@string/home"
android:icon="@drawable/ic_home"/>
<item android:id="@+id/nav_offers"
android:icon="@drawable/ic_my_offers"
android:title="@string/offers"/>
<item
android:id="@+id/nav_aroundme"
android:title="@string/aroundme"
android:icon="@drawable/ic_around_me"/>
</group>
<group android:id="@+id/nav_group_activities">
<item
android:id="@+id/nav_help"
android:title="@string/help"
android:icon="@drawable/ic_help"/>
<item android:id="@+id/nav_settings"
android:title="@string/action_settings"
android:icon="@drawable/ic_settings"/>
<item android:id="@+id/nav_logout"
android:icon="@drawable/ic_disconnect"
android:title="@string/disconnect"/>
</group>
</menu>
为具有您创建的ID的每个组自动插入分隔符
答案 2 :(得分:0)
查看下面的链接,您将获得所需的一切。 导航抽屉的一个非常简单的例子。
http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/