【问题标题】:How do you add images beside text in an arrayList in Android?如何在 Android 的 arrayList 中的文本旁边添加图像?
【发布时间】:2016-04-06 12:20:04
【问题描述】:
 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());
}

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

【问题讨论】:

  • 您可以创建具有图像的自定义布局并将该布局添加到抽屉中
  • 您可以通过使用嵌套类型的布局系统(即一个布局内另一个布局)来自定义列表、选项卡、视图分机
  • 我应该如何进行自定义布局?
  • 还是我应该从头开始使用嵌套类型的系统?
  • 没有办法为每个抽屉项目添加布局吗??

标签: java android xml android-studio


【解决方案1】:

首先你为自定义行创建一个布局文件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>

在你的活动课上 列两个清单

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;
}   

}

现在将此适配器对象设置为您的列表视图

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

【讨论】:

    猜你喜欢
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    相关资源
    最近更新 更多