简体   繁体   English

将按钮添加到导航抽屉片段

[英]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 这是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: 片段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? 可能是因为您在XML文件中两次声明了Android名称空间(xmlns)?

I removed the namespaces from the ListView and put them in the parent RelativeLayout and it works for me. 我从ListView删除了名称空间,并将其放在父级RelativeLayout ,它对我有用。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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