在导航抽屉背景图像和图像视图 [英] Background image and image view in Navigation Drawer
问题描述
我尝试了类似的导航抽屉式设计,Play商店应用:我已经拿到了菜单部分的东西都工作正常,但我想补充的侧面图像和用户ID像下图菜单的顶部。我不知道如何实现这部分:
我有试过以下code和XML中有要显示的项目列表添加它。
<的LinearLayout
机器人:ID =@ + ID / LinearLayoutFrom1
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:重力=左|中心
机器人:背景=@可绘制/ BG
机器人:方向=横向>
< ImageView的
机器人:ID =@ + ID / clear1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:SRC =@可绘制/ profilepic/>
<的TextView
机器人:ID =@ + ID / textViewName
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:重力=中心
机器人:文本=用户名
机器人:文字颜色=#FFFFFF
机器人:TEXTSIZE =15sp
机器人:TEXTSTYLE =黑体/>
< / LinearLayout中>
但这只是附加的背景和文本的每个列表项。
我如何实现上述设计?
更新1:
我尝试了以下的XML:
< android.support.v4.widget.DrawerLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / drawer_layout
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
<的FrameLayout
机器人:ID =@ + ID / framelayoutid
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
<的LinearLayout
机器人:ID =@ + ID / LinearLayoutFrom1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@可绘制/ BG
机器人:方向=垂直>
<的LinearLayout
机器人:ID =@ + ID / LinearLayoutFrom2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向
20:机器人layout_weight =>
< ImageView的
机器人:ID =@ + ID / clear1
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:SRC =@可绘制/ profilepic/>
< / LinearLayout中>
<的LinearLayout
机器人:ID =@ + ID / LinearLayoutFrom3
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =80
机器人:方向=横向>
<的TextView
机器人:ID =@ + ID / textViewName
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:文本=用户名
机器人:文字颜色=#FFFFFF
机器人:TEXTSIZE =15sp
机器人:TEXTSTYLE =黑体/>
< / LinearLayout中>
<的ListView
机器人:ID =@ + ID / left_panel
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_gravity =开始
机器人:背景=@色/列表
机器人:choiceMode =singleChoice
机器人:分隔=@色/除法
机器人:dividerHeight =1DP
机器人:listSelector =@可绘制/选择器/>
< / LinearLayout中>
< /android.support.v4.widget.DrawerLayout>
我收到以下错误:
更新2:
我设法添加页眉通过执行以下列出:
mDrawerList =(ListView控件)findViewById(R.id.left_menu);
查看标题= getLayoutInflater()膨胀(R.layout.headerlayoutfordrawer,空)。
mDrawerList.addHeaderView(头);
navDrawerItems =新的ArrayList< NavDrawerItem>();
navDrawerItems.add(新NavDrawerItem(navMenuTitles [0],navMenuIcons.getResourceId(0,-1)));
navDrawerItems.add(新NavDrawerItem(navMenuTitles [1],navMenuIcons.getResourceId(1,-1),真,2));
navDrawerItems.add(新NavDrawerItem(navMenuTitles [2],navMenuIcons.getResourceId(2,-1)));
navDrawerItems.add(新NavDrawerItem(navMenuTitles [3],navMenuIcons.getResourceId(3,-1)));
navDrawerItems.add(新NavDrawerItem(navMenuTitles [4],navMenuIcons.getResourceId(4,-1)));
navDrawerItems.add(新NavDrawerItem(navMenuTitles [5],navMenuIcons.getResourceId(5,-1)));
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(新SlideMenuClickListener());
适配器=新NavDrawerListAdapter(getApplicationContext(),navDrawerItems);
mDrawerList.setAdapter(适配器);
但问题是,报头也被算在数组列表中DrawerItems因此我得到阵列outofboundexception
11月7号至22号:18:01.433:E / AndroidRuntime(14229):致命异常:主要
11月七号至22日:18:01.433:E / AndroidRuntime(14229):java.lang.ArrayIndexOutOfBoundsException:长度= 6;指数= 7
11月七号至22日:18:01.433:E / AndroidRuntime(14229):在com.jlk.trip.maps.displayView(maps.java:192)
11月七号至22日:18:01.433:E / AndroidRuntime(14229):在com.jlk.trip.maps.access $ 0(maps.java:159)
11月七号至22日:18:01.433:E / AndroidRuntime(14229):在com.jlk.trip.maps.maps $ SlideMenuClickListener.onItemClick(maps.java:122)
11月七号至22日:18:01.433:E / AndroidRuntime(14229):在android.widget.AdapterView.performItemClick(AdapterView.java:298)
11月七号至22日:18:01.433:E / AndroidRuntime(14229):在android.widget.AbsListView.performItemClick(AbsListView.java:1100)
11月七号至22日:18:01.433:E / AndroidRuntime(14229):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:2788)
11月七号至22日:18:01.433:E / AndroidRuntime(14229):在android.widget.AbsListView $ 1.运行(AbsListView.java:3463)
11月七号至22日:18:01.433:E / AndroidRuntime(14229):在android.os.Handler.handleCallback(Handler.java:730)
11月七号至22日:18:01.433:E / AndroidRuntime(14229):在android.os.Handler.dispatchMessage(Handler.java:92)
11月七号至22日:18:01.433:E / AndroidRuntime(14229):在android.os.Looper.loop(Looper.java:137)
11月七号至22日:18:01.433:E / AndroidRuntime(14229):在android.app.ActivityThread.main(ActivityThread.java:5103)
11月七号至22日:18:01.433:E / AndroidRuntime(14229):在java.lang.reflect.Method.invokeNative(本机方法)
11月七号至22日:18:01.433:E / AndroidRuntime(14229):在java.lang.reflect.Method.invoke(Method.java:525)
11月七号至22日:18:01.433:E / AndroidRuntime(14229):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
11月七号至22日:18:01.433:E / AndroidRuntime(14229):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11月七号至22日:18:01.433:E / AndroidRuntime(14229):在dalvik.system.NativeStart.main(本机方法)
看看我的XML在这里: <一href="https://github.com/Sottti/BuyIt/blob/master/BuyIt/src/main/res/layout/activity_main.xml">https://github.com/Sottti/BuyIt/blob/master/BuyIt/src/main/res/layout/activity_main.xml
在那里说:注重你的抽屉。
导航抽屉的第二个孩子android.support.v4.widget.DrawerLayout。
第二个孩子是包含RelativeLayout的和ListView中的LinearLayout。
ListView控件是抽屉式列表中查看它的自我。
的相对布局是其中图像报头位于的地方。
尝试,它为我工作。
LUCK !!
I am trying out a navigation drawer design similar to play store App: I have got the menu part things are working fine but I would like add the profile image and user id in the top of menu like image below. I am not sure how to achieve this part:
I have the tried the following code and added it in xml which has the list of items to be shown.
<LinearLayout
android:id="@+id/LinearLayoutFrom1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left|center"
android:background="@drawable/bg"
android:orientation="horizontal" >
<ImageView
android:id="@+id/clear1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/profilepic" />
<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="UserID"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
but this just appends the background and text for each listitem.
How do I achieve the above design?
UPDATE 1:
I tried the following in the XML:
<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" >
<FrameLayout
android:id="@+id/framelayoutid"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/LinearLayoutFrom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/LinearLayoutFrom2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight=".20" >
<ImageView
android:id="@+id/clear1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/profilepic" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayoutFrom3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".80"
android:orientation="horizontal" >
<TextView
android:id="@+id/textViewName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="UserID"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<ListView
android:id="@+id/left_panel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/list"
android:choiceMode="singleChoice"
android:divider="@color/divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/selector" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
I get the following error:
Update 2:
I managed to add header to list by doing the following:
mDrawerList = (ListView) findViewById(R.id.left_menu);
View header = getLayoutInflater().inflate(R.layout.headerlayoutfordrawer, null);
mDrawerList.addHeaderView(header);
navDrawerItems = new ArrayList<NavDrawerItem>();
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1),true, "2"));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
mDrawerList.setAdapter(adapter);
But the problem is that the header is also being counted in arraylist in DrawerItems and hence I get array outofboundexception
07-22 11:18:01.433: E/AndroidRuntime(14229): FATAL EXCEPTION: main
07-22 11:18:01.433: E/AndroidRuntime(14229): java.lang.ArrayIndexOutOfBoundsException: length=6; index=7
07-22 11:18:01.433: E/AndroidRuntime(14229): at com.jlk.trip.maps.displayView(maps.java:192)
07-22 11:18:01.433: E/AndroidRuntime(14229): at com.jlk.trip.maps.access$0(maps.java:159)
07-22 11:18:01.433: E/AndroidRuntime(14229): at com.jlk.trip.maps.maps$SlideMenuClickListener.onItemClick(maps.java:122)
07-22 11:18:01.433: E/AndroidRuntime(14229): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
07-22 11:18:01.433: E/AndroidRuntime(14229): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
07-22 11:18:01.433: E/AndroidRuntime(14229): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
07-22 11:18:01.433: E/AndroidRuntime(14229): at android.widget.AbsListView$1.run(AbsListView.java:3463)
07-22 11:18:01.433: E/AndroidRuntime(14229): at android.os.Handler.handleCallback(Handler.java:730)
07-22 11:18:01.433: E/AndroidRuntime(14229): at android.os.Handler.dispatchMessage(Handler.java:92)
07-22 11:18:01.433: E/AndroidRuntime(14229): at android.os.Looper.loop(Looper.java:137)
07-22 11:18:01.433: E/AndroidRuntime(14229): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-22 11:18:01.433: E/AndroidRuntime(14229): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 11:18:01.433: E/AndroidRuntime(14229): at java.lang.reflect.Method.invoke(Method.java:525)
07-22 11:18:01.433: E/AndroidRuntime(14229): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-22 11:18:01.433: E/AndroidRuntime(14229): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-22 11:18:01.433: E/AndroidRuntime(14229): at dalvik.system.NativeStart.main(Native Method)
Take a look at my xml here: https://github.com/Sottti/BuyIt/blob/master/BuyIt/src/main/res/layout/activity_main.xml
Pay attention at where it says "YOUR DRAWER".
The Navigation Drawer is the second child in android.support.v4.widget.DrawerLayout.
The second child is a LinearLayout containing a RelativeLayout and a ListView.
The ListView is the Drawer list view it self.
The Relative layout is the place where the image header is located.
Try that, it's working for me.
Luck!!
这篇关于在导航抽屉背景图像和图像视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!