简体   繁体   中英

Navigation drawer with user header in android

try to get this(the header with the picture) : 在此处输入图片说明

And this is my code :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">

<include layout="@layout/toolbar" />

<android.support.v4.widget.DrawerLayout
    android:layout_width="match_parent"
    android:id="@+id/drawerLayout"
    android:layout_height="match_parent">

    <!-- activity view -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:background="#fff"
        android:layout_height="match_parent">

        <TextView
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:textColor="#000"
            android:text="Activity Content"
            android:layout_height="wrap_content" />

        <com.shamanland.fab.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            app:floatingActionButtonColor="#000000"
            app:floatingActionButtonSize="mini"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="20dp"
            />
    </RelativeLayout>

    <!-- navigation drawer -->
    <RelativeLayout
        android:layout_gravity="left|start"
        android:layout_width="match_parent"
        android:background="#fff"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/top_control_bar">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="bababababbababababbababababba"/>
        </RelativeLayout>

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="#eee"
            android:background="#fff"
            android:dividerHeight="1dp" />
    </RelativeLayout>

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

I don't know if i have to create a new layout above the list or something else.

I've seen that there is a listview header in android but this doesn't work in my example. Thanks for your help

You will need to create a simple LinearLayout which contains a List. Here's an example:

 <LinearLayout
android:id="@+id/linearDrawer"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/navigation_items"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    android:id="@+id/userContent"
    android:layout_width="260dp"
    android:layout_height="150dp"
    android:background="@color/black"
    android:gravity="center_vertical"
    android:paddingRight="10dp">

    <RelativeLayout
        android:id="@+id/userDrawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/transparent">

        <ImageView
            android:id="@+id/ImgDrawer"
            android:layout_width="260dp"
            android:layout_height="200dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:contentDescription="@string/dummy_user_description"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_user" />

        <RelativeLayout
            style="@style/textView_title_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true">

            <TextView
                android:id="@+id/txt_user_name_drawer"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/dummy_username"
                android:textAllCaps="true"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white" />

            <TextView
                android:id="@+id/txt_user_lastname_drawer"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/txt_user_name_drawer"
                android:text="@string/dummy_user_description"
                android:textAllCaps="false"
                android:textColor="@color/white"
                android:textSize="12sp" />
        </RelativeLayout>
    </RelativeLayout>
</LinearLayout>

<View
    android:id="@+id/viewSeparator"
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="@color/blue_dark" />

<ListView
    android:id="@+id/listDrawer"
    android:layout_width="260dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/navigation_items"
    android:cacheColorHint="@color/transparent"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="1dp" />

The Layout itself contains the FrameLayout (Content) and the Drawer.

Sample:

 <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.androiddevworker.rateit.view.RatingActivity">

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

<fragment
    android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="fill_parent"
    android:layout_gravity="start"
    android:name="com.androiddevworker.rateit.view.fragments.RatingNavigationDrawerFragment"
    tools:layout="@layout/navigation_main_include" />

In your activity you can get it by using

    private RatingNavigationDrawerFragment mRatingNavigationDrawerFragment;

    mRatingNavigationDrawerFragment = (RatingNavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);

while the RatingNavigationDrawerfragment (example) is just a simple fragment.

Try the Below code......

  <RelativeLayout
    android:layout_gravity="left|start"
    android:layout_width="match_parent"
    android:background="#fff"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/top_control_bar">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="bababababbababababbababababba"/>
    </RelativeLayout>

     <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/top_control_bar1">
        <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/ur_image" />
      <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="start"
                    android:text="Your Text"
                    android:textColor="#FFFFFF"
                    android:textSize="38px" />

    </RelativeLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/top_control_bar1"
        android:divider="#eee"
        android:background="#fff"
        android:dividerHeight="1dp" />
</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