ListView单个项目样式

时间:2016-12-22 07:22:09

标签: android

我正在开发一个Android应用程序,在一个片段中,我已经有了一个列表视图,并且有一组项目将通过arrayadapter添加到此列表视图中。但是,我被要求根据原始数据库中的特定属性动态地为某些列表项着色,特别是仅粗体显示那些要突出显示它们的元素。是否有任何可能的方法来使用arrayAdapter类来实现这一点,或者如果没有,考虑到这是一个简单的用例,我该怎么做才能实现这个目标

4 个答案:

答案 0 :(得分:2)

处理这个问题非常容易,只需要在适配器中添加一个条件,如果条件为真,你只需根据自己的要求自定义项目。

  if(condition)
    {
        view.setBackgroundColor(activity.getResources().getColor(R.color.White));

    }

如果您想在您的适配器中以编程方式将TextView设置为Bold,请检查您的状况并将其设置如下:

textView.setTypeface(null, Typeface.BOLD);

答案 1 :(得分:1)

你可以通过ArrayAdapter类来实现, 只需在适配器getView方法中添加条件,并将文本视图设置为粗体

if(conditionPart){
    textView.setTypeface(null, Typeface.BOLD); // For bold
    textView.setBackgroundColor(context.getResources().getColor(R.color.Black)); // For Black Color Background
}else{
    textView.setTypeface(null, Typeface.NORMAL);// For Noral
    textView.setBackgroundColor(context.getResources().getColor(R.color.Gray)); // For Gray Color Background
}

答案 2 :(得分:1)

  1. 请参阅本教程: https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView

  2. 扩展ArrayAdapter类并覆盖getView()方法

  3. 如上所述应用条件陈述

答案 3 :(得分:0)

您可以创建custom Adapter,您可以根据需要设置文本样式,这是自定义适配器的示例: Custom Adapter for List View