我在我的Android应用程序中使用NavigationDrawer来显示ListView中的项目。我想要做的是以编程方式添加ImageView元素。
我的NavigationDrawer布局(activity_navigation.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/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/left_linear"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start">
<ListView
android:id="@+id/left_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@color/navigation_divider"
android:dividerHeight="1dp" />
</LinearLayout>
我也有一个ListView项目布局(listview_item_row.xml
)。它是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/navigation_drawer_list_item_selector"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:padding="10dp" >
<ImageView
android:id="@+id/imageViewIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingRight="10dp" />
<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/imageViewIcon"
android:paddingRight="10dp"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#ffffff" />
</RelativeLayout>
在我的NavigationActivity中,我使用以下代码动态填充ListView项目:
mNavigationDrawerItemTitles = getResources().getStringArray(R.array.app_navigation_items);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLinear = (LinearLayout) findViewById(R.id.left_linear);
ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[6];
drawerItem[0] = new ObjectDrawerItem(R.drawable.ic_speedometer, mNavigationDrawerItemTitles[0]);
drawerItem[1] = new ObjectDrawerItem(R.drawable.ic_place, mNavigationDrawerItemTitles[1]);
drawerItem[2] = new ObjectDrawerItem(R.drawable.ic_price, mNavigationDrawerItemTitles[2]);
drawerItem[3] = new ObjectDrawerItem(R.drawable.ic_action_settings, mNavigationDrawerItemTitles[3]);
drawerItem[4] = new ObjectDrawerItem(R.drawable.ic_about, mNavigationDrawerItemTitles[4]);
drawerItem[5] = new ObjectDrawerItem(R.drawable.ic_logout, mNavigationDrawerItemTitles[5]);
DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.listview_item_row, drawerItem);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
那么,我如何在NavigationDrawer中实现ImageView以及其中的名称和图片?
答案 0 :(得分:1)
您可以使用ListView.addHeaderView(View)
在ListView顶部添加视图。这将使图片和名称与ListView
一起滚动。
headerView:
的示例mDrawerList.setAdapter(adapter);
View headerView = LayoutInflater.from(this).inflate(R.layout.listview_item_row);
((ImageView) headerView.findViewById(R.id.imageViewIcon)).setImageBitmap(...);
((TextView) headerView.findViewById(R.id.textviewName)).setText(userName);
mDrawerList.addHeaderView(headerView);
如果您不希望图片和名称滚动,只需在activity_navigation.xml
答案 1 :(得分:0)
<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"
style="@style/activity_base">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/content_frame"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar android:id="@+id/progressbar"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-7dp"
style="@style/ProgressBarblitzprogress"/>
</RelativeLayout>
<LinearLayout
android:id="@+id/dlinlayout"
android:layout_width="240dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start"
android:background="@color/ehog_green"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:paddingBottom="2dp"
android:paddingRight="2dp"
android:paddingLeft="4dp"
android:background="@drawable/bottom_line"
>
<ImageView
android:id="@+id/current_country"
android:src="@drawable/_cf_big_empty"
android:layout_alignParentLeft="true"
android:layout_width="44dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="1dp"
/>
<TextView
android:id="@+id/curent_settlem"
android:text="@string/unknown_place"
style="@style/current_settlem_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/current_country"
android:layout_alignParentTop="true"
android:singleLine="true"
/>
<TextView
android:id="@+id/curent_address"
android:text="@string/unknown_address"
style="@style/current_address_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/current_country"
android:layout_below="@+id/curent_settlem"
android:singleLine="true"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:id="@+id/current_sunangle_pic"
android:layout_below="@+id/curent_address"
android:layout_toRightOf="@+id/current_country"
/>
<TextView
android:id="@+id/current_sunangle"
android:text="@string/unknown_place_sunangle"
style="@style/current_address_sunangle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/current_sunangle_pic"
android:layout_below="@+id/current_country"
android:singleLine="true"
android:layout_marginBottom="3dp"
/>
</RelativeLayout>
<ListView
android:id="@+id/menu_items"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:listSelector="@drawable/menuitem_selector"
>
</ListView>
</LinearLayout>
这是自定义导航抽屉的示例,导航的主要布局是dlinlayout
,标题是此内的RelativeLayout
。我还为left_drawer
实现了一个自定义列表适配器,以显示带有图标的菜单。