我有一个带有一些列表项的导航抽屉.如何在每个列表项上添加图标并更改文本颜色,背景颜色.
这是我的代码
Java的
mDrawerListVIEw.setAdapter(new ArrayAdapter<String>( getActionbar().getthemedContext(), androID.R.layout.simple_List_item_activated_1, androID.R.ID.text1, new String[]{ getString(R.string.Title_section1), getString(R.string.Title_section2), getString(R.string.Title_section3), }));
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:choiceMode="singleChoice" androID:divIDer="@androID:color/transparent" androID:divIDerHeight="0dp" androID:background="#cc0100cc" tools:context="com.rupomkhondaker.sonalibanklimited.NavigationDrawerFragment" androID:ID="@+ID/menuList" />
NavigationDrawerFragment.java请在此处输入以下答案
package com.rupomkhondaker.sonalibanklimited;import androID.support.v7.app.AppCompatActivity;import androID.app.Activity;import androID.support.v7.app.Actionbar;import androID.support.v4.app.Fragment;import androID.support.v4.app.ActionbarDrawerToggle;import androID.support.v4.vIEw.GravityCompat;import androID.support.v4.Widget.DrawerLayout;import androID.content.SharedPreferences;import androID.content.res.Configuration;import androID.os.Bundle;import androID.preference.PreferenceManager;import androID.vIEw.LayoutInflater;import androID.vIEw.Menu;import androID.vIEw.MenuInflater;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.AdapterVIEw;import androID.Widget.ArrayAdapter;import androID.Widget.ListVIEw;import androID.Widget.Toast;public class NavigationDrawerFragment extends Fragment { /** * Remember the position of the selected item. */ private static final String STATE_SELECTED_position = "selected_navigation_drawer_position"; /** * Per the design guIDelines, you should show the drawer on launch until the user manually * expands it. This shared preference tracks this. */ private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned"; /** * A pointer to the current callbacks instance (the Activity). */ private NavigationDrawerCallbacks mCallbacks; /** * Helper component that tIEs the action bar to the navigation drawer. */ private ActionbarDrawerToggle mDrawerToggle; private DrawerLayout mDrawerLayout; private ListVIEw mDrawerListVIEw; private VIEw mFragmentContainerVIEw; private int mCurrentSelectedposition = 0; private boolean mFromSavedInstanceState; private boolean mUserLearnedDrawer; public NavigationDrawerFragment() { } @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Read in the flag indicating whether or not the user has demonstrated awareness of the // drawer. See PREF_USER_LEARNED_DRAWER for details. SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity()); mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false); if (savedInstanceState != null) { mCurrentSelectedposition = savedInstanceState.getInt(STATE_SELECTED_position); mFromSavedInstanceState = true; } // Select either the default item (0) or the last selected item. selectItem(mCurrentSelectedposition); } @OverrIDe public voID onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); // Indicate that this fragment would like to influence the set of actions in the action bar. setHasOptionsMenu(true); } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { mDrawerListVIEw = (ListVIEw) inflater.inflate( R.layout.fragment_navigation_drawer, container, false); mDrawerListVIEw.setonItemClickListener(new AdapterVIEw.OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { selectItem(position); } }); mDrawerListVIEw.setAdapter(new ArrayAdapter<String>(getActionbar().getthemedContext(), androID.R.layout.simple_List_item_activated_1, androID.R.ID.text1, new String[]{ getString(R.string.menu_HOME), getString(R.string.menu_PhoneBook), getString(R.string.menu_About), getString(R.string.menu_Services), getString(R.string.menu_Remittance), getString(R.string.menu_Branch), getString(R.string.menu_Islam), getString(R.string.menu_Misc), getString(R.string.menu_Career), getString(R.string.menu_Devinfo), } )); mDrawerListVIEw.setItemChecked(mCurrentSelectedposition, true); return mDrawerListVIEw; } public boolean isDraweropen() { return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerVIEw); } /** * Users of this fragment must call this method to set up the navigation drawer interactions. * * @param fragmentID The androID:ID of this fragment in its activity's layout. * @param drawerLayout The DrawerLayout containing this fragment's UI. */ public voID setUp(int fragmentID, DrawerLayout drawerLayout) { mFragmentContainerVIEw = getActivity().findVIEwByID(fragmentID); mDrawerLayout = drawerLayout; // set a custom shadow that overlays the main content when the drawer opens mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); // set up the drawer's List vIEw with items and click Listener Actionbar actionbar = getActionbar(); actionbar.setdisplayHomeAsUpEnabled(true); actionbar.setHomebuttonEnabled(true); // ActionbarDrawerToggle tIEs together the the proper interactions // between the navigation drawer and the action bar app icon. mDrawerToggle = new ActionbarDrawerToggle( getActivity(), /* host Activity */ mDrawerLayout, /* DrawerLayout object */ R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */ R.string.navigation_drawer_open, /* "open drawer" description for accessibility */ R.string.navigation_drawer_close /* "close drawer" description for accessibility */ ) { @OverrIDe public voID onDrawerClosed(VIEw drawerVIEw) { super.onDrawerClosed(drawerVIEw); if (!isAdded()) { return; } getActivity().supportInvalIDateOptionsMenu(); // calls onPrepareOptionsMenu() } @OverrIDe public voID onDrawerOpened(VIEw drawerVIEw) { super.onDrawerOpened(drawerVIEw); if (!isAdded()) { return; } if (!mUserLearnedDrawer) { // The user manually opened the drawer; store this flag to prevent auto-showing // the navigation drawer automatically in the future. mUserLearnedDrawer = true; SharedPreferences sp = PreferenceManager .getDefaultSharedPreferences(getActivity()); sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply(); } getActivity().supportInvalIDateOptionsMenu(); // calls onPrepareOptionsMenu() } }; // If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer, // per the navigation drawer design guIDelines. if (!mUserLearnedDrawer && !mFromSavedInstanceState) { mDrawerLayout.openDrawer(mFragmentContainerVIEw); } // Defer code dependent on restoration of prevIoUs instance state. mDrawerLayout.post(new Runnable() { @OverrIDe public voID run() { mDrawerToggle.syncState(); } }); mDrawerLayout.setDrawerListener(mDrawerToggle); } private voID selectItem(int position) { mCurrentSelectedposition = position; if (mDrawerListVIEw != null) { mDrawerListVIEw.setItemChecked(position, true); } if (mDrawerLayout != null) { mDrawerLayout.closeDrawer(mFragmentContainerVIEw); } if (mCallbacks != null) { mCallbacks.onNavigationDrawerItemSelected(position); } } @OverrIDe public voID onAttach(Activity activity) { super.onAttach(activity); try { mCallbacks = (NavigationDrawerCallbacks) activity; } catch (ClassCastException e) { throw new ClassCastException("Activity must implement NavigationDrawerCallbacks."); } } @OverrIDe public voID onDetach() { super.onDetach(); mCallbacks = null; } @OverrIDe public voID onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt(STATE_SELECTED_position, mCurrentSelectedposition); } @OverrIDe public voID onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Forward the new configuration the drawer toggle component. mDrawerToggle.onConfigurationChanged(newConfig); } @OverrIDe public voID onCreateOptionsMenu(Menu menu, MenuInflater inflater) { // If the drawer is open, show the global app actions in the action bar. See also // showGlobalContextActionbar, which controls the top-left area of the action bar. if (mDrawerLayout != null && isDraweropen()) { inflater.inflate(R.menu.global, menu); showGlobalContextActionbar(); } super.onCreateOptionsMenu(menu, inflater); } @OverrIDe public boolean onoptionsItemSelected(MenuItem item) { if (mDrawerToggle.onoptionsItemSelected(item)) { return true; } if (item.getItemID() == R.ID.action_example) { Toast.makeText(getActivity(), "Example action.", Toast.LENGTH_SHORT).show(); return true; } return super.onoptionsItemSelected(item); } /** * Per the navigation drawer design guIDelines, updates the action bar to show the global app * 'context', rather than just what's in the current screen. */ private voID showGlobalContextActionbar() { Actionbar actionbar = getActionbar(); actionbar.setdisplayShowTitleEnabled(true); actionbar.setNavigationMode(Actionbar.NAVIGATION_MODE_STANDARD); actionbar.setTitle(R.string.app_name); } private Actionbar getActionbar() { return ((AppCompatActivity) getActivity()).getSupportActionbar(); } /** * Callbacks interface that all activitIEs using this fragment must implement. */ public static interface NavigationDrawerCallbacks { /** * Called when an item in the navigation drawer is selected. */ voID onNavigationDrawerItemSelected(int position); }}
解决方法:
为此,您需要创建自定义适配器.您可以在下面的链接中找到自定义适配器的实现,
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
上面包含一个很好的自定义适配器教程,可以在每个列表行中添加图标,更改文本和背景的颜色….
这里也是关于自定义适配器的好教程,
http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92
这是一个例子,
你的活动的xml应该像这个activity_main.xml,
<relativeLayout 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" tools:context="com.rupomkhondaker.sonalibanklimited.NavigationDrawerFragment" /><ListVIEw androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:choiceMode="singleChoice" androID:divIDer="@androID:color/transparent" androID:divIDerHeight="0dp" androID:background="#cc0100cc" androID:ID="@+ID/menuList" /></relativeLayout>
你的Activity类:
公共类MainActivity扩展Activity {
private ListVIEw ListVIEw1;@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); String[] textString = {"Item1", "Item2", "Item3", "Item4"}; int[] drawableIDs = {R.drawable.img_ID_row1, R.drawable.img_ID_row2, R.drawable.img_ID_row3, R.drawable.img_ID_row4}; CustomAdapter adapter = new CustomAdapter(this, textString, drawableIDs); ListVIEw1 = (ListVIEw)findVIEwByID(R.ID.menuList); ListVIEw1.setAdapter(adapter);}
为每行创建另一个布局,如下所示,并将其命名为row.xml,
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="horizontal" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:padding="10dp"> <ImageVIEw androID:ID="@+ID/imgIcon" androID:layout_wIDth="wrap_content" androID:layout_height="fill_parent" androID:gravity="center_vertical" androID:layout_alignParenttop="true" androID:layout_alignParentBottom="true" androID:layout_marginRight="15dp" androID:layout_margintop="5dp" androID:layout_marginBottom="5dp" /> <TextVIEw androID:ID="@+ID/txtTitle" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:gravity="center_vertical" androID:layout_alignParenttop="true" androID:layout_alignParentBottom="true" androID:textStyle="bold" androID:textSize="22dp" androID:textcolor="#000000" androID:layout_margintop="5dp" androID:layout_marginBottom="5dp" /></linearLayout>
现在,创建一个java类并将其命名为CustomAdapter.java:
public class CustomAdapter extends BaseAdapter { private Context mContext; private String[] Title; private int[] imge; public CustomAdapter(Context context, String[] text1,int[] imageIDs) { mContext = context; Title = text1; imge = imageIDs; } public int getCount() { // Todo auto-generated method stub return Title.length; } public Object getItem(int arg0) { // Todo auto-generated method stub return null; } public long getItemID(int position) { // Todo auto-generated method stub return position; } public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { LayoutInflater inflater = getLayoutInflater(); VIEw row; row = inflater.inflate(R.layout.row, parent, false); TextVIEw Title; ImageVIEw i1; i1 = (ImageVIEw) row.findVIEwByID(R.ID.imgIcon); Title = (TextVIEw) row.findVIEwByID(R.ID.txtTitle); Title.setText(Title[position]); i1.setimageResource(imge[position]); return (row); } }
总结 以上是内存溢出为你收集整理的java – Android如何在每个listvIew列表项上添加图标并更改文本颜色,背景颜色全部内容,希望文章能够帮你解决java – Android如何在每个listvIew列表项上添加图标并更改文本颜色,背景颜色所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)