【发布时间】:2015-08-27 14:28:51
【问题描述】:
我成功创建了一个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(显示菜单的activity使用的,未修改)
<!-- 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));
}
我做错了什么?
【问题讨论】:
-
你的 Logcat 说异常发生在哪一行,那一行究竟是什么?
-
刚刚添加到版本中
-
您能否为您的活动发布
onCreate(),因为这是发生错误的地方? -
为什么你的导航抽屉是一个片段?您可以在主要活动上使用布局。
-
我建议使用android support design library 及其
NavigationView。这样你就可以很容易地创建一个好看的抽屉。
标签: android android-listview navigation-drawer