gpt4 book ai didi

java - 你如何在 Android 的 arrayList 中的文本旁边添加图像?

转载 作者:行者123 更新时间:2023-11-30 10:51:52 24 4
gpt4 key购买 nike

 private void initDrawerList() {
// initialize drawer content
// need to determine selected item according to the currently selected sensor type
drawerItemList = new ArrayList();

drawerItemList.add(new DrawerItem("*1st text", R.drawable.my_sensz_normal, R.drawable.my_sensz_selected, true));
drawerItemList.add(new DrawerItem("*2nd text", R.drawable.friends_normal, R.drawable.friends_selected, false));
drawerItemList.add(new DrawerItem("*3rd text", R.drawable.friends_normal, R.drawable.friends_selected, false));

drawerAdapter = new DrawerAdapter(HomeActivity.this, drawerItemList);
drawerListView = (ListView) findViewById(R.id.drawer);

if (drawerListView != null)
drawerListView.setAdapter(drawerAdapter);

drawerListView.setOnItemClickListener(new DrawerItemClickListener());
}

我想用图片替换 *,但我不知道该怎么做。有人可以帮忙吗?谢谢! :)

最佳答案

首先为自定义行创建一个布局文件navigation_row.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:orientation="horizontal"
android:gravity="center"
android:padding="10dip" >

<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Icon"
android:src="@drawable/order_64" />

<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Example application"
android:textColor="@color/whiteColor"
android:paddingLeft="10dp"
android:textSize="20sp" />

</LinearLayout>

在你的 Activity 课上制作两个列表

int [] icon = {R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.img4,
R.drawable.img5};

String [] drawerTitle ={"1st text",
"2 text",
"3 text",
"4 text",
"5 text"};

创建适配器类

public class NavigationAdapter extends ArrayAdapter <String> {
private final Context context;
private final String[] titles;
private final int[] icon;

public NavigationAdapter(Context context, int[] icon, String[] titles){
super(context, R.layout.navigation_row,titles);
this.context=context;
this.icon=icon;
this.titles=titles;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.navigation_row,parent,false);

ImageView imageView=(ImageView)rowView.findViewById(R.id.icon);
TextView textView = (TextView)rowView.findViewById(R.id.name);

imageView.setImageResource(icon[position]);
textView.setText(titles[position]);

return rowView;
}

现在将此适配器对象设置为您的 ListView

 NavigationAdapter onAdapter=new NavigationAdapter(Navigation.this,icon,drawerTitle);
drawerListView.setAdapter(onAdapter);
drawerListView.setOnItemClickListener(new DrawerItemClickListener());

关于java - 你如何在 Android 的 arrayList 中的文本旁边添加图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34564862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com