Add header Image in navigation drawer on android studio sample navigation drawer

僤鯓⒐⒋嵵緔 提交于 2019-12-13 18:37:53

问题


i need to add header image like this Post :

can I add an image header in navigation drawer layout

but, im using android studio sample navigation drawer for doing this.

it is good or bad for design this ?

and the better way or Compact way for doing this ?

any suggestion ?

thanks.


回答1:


Can I add an Image Header in Navigation Drawer Layout?

Yes you can add with no problems.

If you use android studio navigation drawer or some thing else every thing are same. Some additional methods will be present and you need to check the methods required by you and delete other methods. You may also need to add few methods.

As per the latest android, best way of designing navigation drawer is by using recycler view and by using material design.

There are lot of materials in google for doing this, one of the finest tutorials I prefer is in the link given below you can have a look:

https://www.youtube.com/watch?v=zWpEh9k8i7Q&list=PLonJJ3BVjZW6CtAMbJz1XD8ELUs1KXaTD&index=6

Hope it helps.




回答2:


In the menudrawer activity setcontentview(layout).Inside this layout

try to add an imageview.My sample xml is as folllows

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

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:background="@android:color/transparent"
        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.
         The drawer is given a fixed width in dp and extends the full height of
         the container. A solid background is used for contrast
         with the content view. -->

         <LinearLayout 
    android:layout_height="match_parent"
    android:layout_width="120dp"
       android:weightSum="7"
         android:layout_gravity="start"
       android:id="@+id/parentlayoutt"
        android:choiceMode="singleChoice"
       android:orientation="vertical"

     >

<ImageView
android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="3"
     android:id="@+id/headerimage"
  android:src="@drawable/yourimage"

/>
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
     android:id="@+id/parentmenuone"
     android:soundEffectsEnabled="true"
     android:descendantFocusability="blocksDescendants"

      android:background="@color/pure_white"
    >

        <ImageButton 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:src="@drawable/birdwhite"
           android:background="@color/white"
           android:id="@+id/sidejive1"

         android:clickable="false"
            android:layout_centerInParent="true"          
            />


  <TextView 
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/sidejive1"
       android:id="@+id/title1"
        android:clickable="false"
       android:textColor="@color/violetbg"

       android:text="@string/createmenu"

       />

</RelativeLayout>
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:descendantFocusability="blocksDescendants"
     android:id="@+id/parentmenutwo"
      android:background="@drawable/whitebox"
    >

        <ImageButton 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:src="@drawable/profilewhite"
           android:background="@color/white"
           android:id="@+id/sidejive2"
            android:clickable="false"


            android:layout_centerInParent="true"          
            />


  <TextView 
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/sidejive2"
       android:id="@+id/title2"
        android:clickable="false"
       android:textColor="@color/violetbg"

       android:text="@string/Accountmenu"

       />

</RelativeLayout>
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:descendantFocusability="blocksDescendants"
     android:id="@+id/parentmenuthree"
      android:background="@drawable/whitebox"
    >

        <ImageButton 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:src="@drawable/homewhite"
           android:background="@color/white"
           android:id="@+id/sidejive3"
          android:clickable="false"

            android:layout_centerInParent="true"          
            />


  <TextView 
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/sidejive3"
       android:id="@+id/title3"
       android:textColor="@color/violetbg"

       android:text="@string/Dashboardmenu"
      android:clickable="false"
       />

</RelativeLayout>
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
     android:id="@+id/parentmenufour"
     android:descendantFocusability="blocksDescendants"
      android:background="@drawable/whitebox"
    >

        <ImageButton 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:src="@drawable/examplewhite"
           android:background="@color/white"
           android:id="@+id/sidejive4"
          android:clickable="false"

            android:layout_centerInParent="true"          
            />


  <TextView 
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/sidejive4"
       android:id="@+id/title4"
       android:textColor="@color/violetbg"

       android:text="@string/Examplemenu"
      android:clickable="false"
       />

</RelativeLayout>
</LinearLayout>

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


来源:https://stackoverflow.com/questions/29360784/add-header-image-in-navigation-drawer-on-android-studio-sample-navigation-drawer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!