将 ListView 和 TextView 放在 NavigationDrawerFragment 中
Put a ListView and TextView in a NavigationDrawerFragment
我使用AndroidStudio提供的创建滑动菜单的例子成功创建了一个ListView并显示为滑动菜单
现在我想在 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 class(仅修改了 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() 上,特别是 activity 我想显示菜单中的 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));
}
我做错了什么?
您试图将片段膨胀 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" -->
您可以将此布局(导航抽屉内容)添加到您的 activity:
<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>
此模式已通过 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 为每个组自动插入一个分隔符
查看下面的 link,您将获得所需的一切。
一个非常简单的导航抽屉示例。
http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
我使用AndroidStudio提供的创建滑动菜单的例子成功创建了一个ListView并显示为滑动菜单
现在我想在 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 class(仅修改了 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() 上,特别是 activity 我想显示菜单中的 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));
}
我做错了什么?
您试图将片段膨胀 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" -->
您可以将此布局(导航抽屉内容)添加到您的 activity:
<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>
此模式已通过 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 为每个组自动插入一个分隔符
查看下面的 link,您将获得所需的一切。 一个非常简单的导航抽屉示例。
http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/