且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android如何在每个列表列表项上添加图标并更改文本颜色,背景色

更新时间:2023-12-05 16:32:52

为此,您需要创建一个自定义适配器.您可以在以下链接中找到自定义适配器的实现,

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,

For that purpose, you need to create a custom adapter. You can find the implementation of custom adapter on the below link,

http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

Above contain a nice tutorial on custom adapter to add icons, change colors of text and background on each list row....!

Here is also a good tutorial on custom adapter,

http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92

Here is a example,

Your activity's xml should be like this 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>

您的活动课:公共类MainActivity扩展了活动{

Your Activity class: public class MainActivity extends Activity {

private ListView listView1;

@Override
public 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,

Create another layout for each row like below and name it 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:

Now, create a JAVA class and name it 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);
        }
    }