在列表中的每个项旁边添加图标

时间:2014-11-10 16:30:20

标签: java android android-layout android-activity

我有一个字符串列表项,并希望在NavigationDrawer中为每个项目添加一个图标。

以下是名单项目:

OptionMenu = new String[] { "Discover activities", "Matches", "City selection", "Preferences", "Contact Dooba" };
         mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
         mDrawerRelativeLayout = (RelativeLayout)
         findViewById(R.id.left_drawer);
         mDrawerList = (ListView) findViewById(R.id.list_view_drawer);
         mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
         mOptionMenu));

         mDrawerList.setOnItemClickListener(new OnItemClickListener() {
         @Override
         public void onItemClick(AdapterView<?> parent, View view,
         int position, long id) {

         Fragment fragment = null;

         switch (position) {
         case 0:
         fragment = new FirstFragment();
         break;
         case 1:
         fragment = new SecondFragment();
         break;
         case 2:
         fragment = new ThirdFragment();
         break;
         }

         FragmentManager fragmentManager = getSupportFragmentManager();

         fragmentManager.beginTransaction()
         .replace(R.id.content_frame, fragment).commit();

         mDrawerList.setItemChecked(position, true);

         mTitleSection = mOptionMenu[position];
         getSupportActionBar().setTitle(mTitleSection);

         mDrawerLayout.closeDrawer(mDrawerRelativeLayout);
         }
         });
         mDrawerList.setItemChecked(0, true);
         mTitleSection = getTitle();
         mTitleApp = getTitle();

         mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
         R.drawable.logo8, R.string.drawer_open,
         R.string.drawer_close) {

         public void onDrawerClosed(View view) {
             getSupportActionBar().setTitle(mTitleSection);


         ActivityCompat.invalidateOptionsMenu(MainActivity.this);
         }

         public void onDrawerOpened(View drawerView) {
             getSupportActionBar().setTitle(mTitleSection);
         ActivityCompat.invalidateOptionsMenu(MainActivity.this);

         }
         };

         mDrawerLayout.setDrawerListener(mDrawerToggle);

         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
         getSupportActionBar().setHomeButtonEnabled(true);
         getSupportActionBar().setDisplayUseLogoEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main_activity_actions, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        switch (item.getItemId()) {
        case R.id.action_settings:
            Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
            ;
            break;
        default:
            return super.onOptionsItemSelected(item);
        }

        return true;
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

我也想知道如何在项目列表字符串旁边添加数字。例如,匹配(编号22) 非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

使用具有imageview和textview的布局编写自定义适配器和膨胀列表。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_gravity="center"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="TextView" />

</LinearLayout>


public class CustomAdapter extends BaseAdapter{ 
    String [] result;
    Context context;
 int [] imageId;
      private static LayoutInflater inflater=null;
    public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
        // TODO Auto-generated constructor stub
        result=prgmNameList;
        context=mainActivity;
        imageId=prgmImages;
         inflater = ( LayoutInflater )context.
                 getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return result.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class Holder
    {
        TextView tv;
        ImageView img;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder=new Holder();
        View rowView;        
             rowView = inflater.inflate(R.layout.program_list, null);
             holder.tv=(TextView) rowView.findViewById(R.id.textView1);
             holder.img=(ImageView) rowView.findViewById(R.id.imageView1);       
         holder.tv.setText(result[position]);
         holder.img.setImageResource(imageId[position]);         
         rowView.setOnClickListener(new OnClickListener() {         
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
            }
        });  
        return rowView;
    }

}

答案 1 :(得分:0)

使用自定义adapterlayout是一个很好的解决方案,但如果你想要的只是添加一个图标,你可以使用setCompoundDrawablesWithIntrinsicBounds,它最多可以向{{3}添加4个drawable {1}}。

您只需覆盖已使用的TextView的{​​{1}}方法:

getView