简体   繁体   中英

Add button to navigation drawer fragment

I want to add a simple to navigation drawer menu.

This is the fragment definition on main_activity.xml

<fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="eu.foulidis.giannis.agiospaisios.NavigationDrawerFragment"
        tools:layout="@layout/fragment_navigation_drawer" />

The fragments ui:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">


    <Button
        android:layout_width="wrap_content"
        android:id="@+id/btnLogin"
        android:text="@string/com_facebook_picker_done_button_text"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <ListView 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" android:choiceMode="singleChoice"
        android:divider="@android:color/transparent" android:dividerHeight="0dp"
        android:background="#ff6e1f18" tools:context=".NavigationDrawerFragment"
        android:layout_above="@id/btnLogin"
        android:layout_alignParentTop="true"
        android:id="@+id/myListView"
        />
</RelativeLayout>


 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

       mDrawerListView =  (ListView)  inflater.inflate(
               R.layout.fragment_navigation_drawer, container, false).findViewById(R.id.myListView);

Without the button it works but when I add the button I got an exception:

android.view.InflateException: Binary XML file line #25: Error inflating class fragment

Any ideas what might be the problem ?

Could it be because you are declaring the Android namespaces (xmlns) twice in the XML file?

I removed the namespaces from the ListView and put them in the parent RelativeLayout and it works for me.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">


    <Button
        android:layout_width="wrap_content"
        android:id="@+id/btnLogin"
        android:text="@string/com_facebook_picker_done_button_text"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <ListView android:layout_width="match_parent"
        android:layout_height="match_parent" android:choiceMode="singleChoice"
        android:divider="@android:color/transparent" android:dividerHeight="0dp"
        android:background="#ff6e1f18" tools:context=".NavigationDrawerFragment"
        android:layout_above="@id/btnLogin"
        android:layout_alignParentTop="true"
        android:id="@+id/myListView"
    />

</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM