片段导航抽屉中的静态标头

时间:2014-08-06 12:33:03

标签: android xml android-fragments header navigation-drawer

enter image description here我在adt中创建了一个Android项目,导航类型为adt中提供的导航抽屉。我想添加一个静态标题Edittext小部件到抽屉非滚动能够和xml列表中导航抽屉列表视图顶部不可移动将是可滚动的但标题应始终保持静态,任何帮助将是最受欢迎的。我是否需要上传xml的代码我觉得它没用,因为问题很简单但是如果有人想要xml代码问我,我会上传它。

这是我的activity_main.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="com.example.appname.MainActivity" >

<!--
     As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions.
-->

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!--
     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:name="com.example.appname.NavigationDrawerFragment"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start" />

    </android.support.v4.widget.DrawerLayout>

这是我的fragment_navigation_drawer.xml

<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:background="#eee"
android:choiceMode="singleChoice"
android:divider="@color/line_divider"
android:focusableInTouchMode="true"
android:dividerHeight="1dp"
android:padding="5dp"
tools:context="com.example.appname.NavigationDrawerFragment" />

logcat的

08-07 10:20:59.274: E/AndroidRuntime(22577):    FATAL EXCEPTION: main
08-07 10:20:59.274: E/AndroidRuntime(22577):    java.lang.ClassCastException:    android.widget.RelativeLayout$LayoutParams cannot be cast to    android.support.v4.widget.DrawerLayout$LayoutParams

08-07 10:20:59.274: E/AndroidRuntime(22577):    at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:910)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at    android.support.v4.widget.DrawerLayout.isDrawerOpen(DrawerLayout.java:1156)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at com.example.appname.NavigationDrawerFragment.isDrawerOpen(NavigationDrawerFragment.java:148)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at com.example.appname.MainActivity.onCreateOptionsMenu(MainActivity.java:141)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at android.app.Activity.onCreatePanelMenu(Activity.java:2490)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:146)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:293)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:407)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:769)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:201)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at android.os.Handler.handleCallback(Handler.java:725)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at android.os.Handler.dispatchMessage(Handler.java:92)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at android.os.Looper.loop(Looper.java:137)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at android.app.ActivityThread.main(ActivityThread.java:5039)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at java.lang.reflect.Method.invokeNative(Native Method)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at java.lang.reflect.Method.invoke(Method.java:511)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

08-07 10:20:59.274: E/AndroidRuntime(22577):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

NavigationDrawer不一定是ListView,它可以是任何内容。在你的情况下尝试这样的事情:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/flFragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer -->
    <RelativeLayout
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111">

        <EditText
            android:id="@+id/etHeader"
            android:inputType="text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ListView
            android:id="@+id/lvDrawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"
            android:layout_below="@id/etHeader"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />
    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>