Android 如何更改ListView项目的颜色

Android 如何更改ListView项目的颜色,android,listview,Android,Listview,我有一个包含硬编码项目的列表,默认情况下是白色的,在我的白色背景下几乎看不见。 公共类消息扩展ListFragment{ /** An array of items to display in ArrayList */ String android_versions[] = new String[]{ "Jelly Bean", "IceCream Sandwich", "HoneyComb", "Ginger Bread", "Froyo" }

我有一个包含硬编码项目的列表,默认情况下是白色的,在我的白色背景下几乎看不见。 公共类消息扩展ListFragment{

/** An array of items to display in ArrayList */
String android_versions[] = new String[]{
    "Jelly Bean",
    "IceCream Sandwich",
    "HoneyComb",
    "Ginger Bread",
    "Froyo"     
};


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    /** Creating array adapter to set data in listview */
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_multiple_choice, android_versions);

    /** Setting the array adapter to the listview */
    setListAdapter(adapter);

    return super.onCreateView(inflater, container, savedInstanceState);
}



@Override
public void onStart() {
    super.onStart();

    /** Setting the multiselect choice mode for the listview */
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);       
}
/**要在ArrayList中显示的项目数组*/
字符串android_版本[]=新字符串[]{
“果冻豆”,
“冰淇淋三明治”,
“蜂巢”,
“姜面包”,
“弗罗约”
};
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
/**正在创建阵列适配器以在listview中设置数据*/
ArrayAdapter=新的ArrayAdapter(getActivity().getBaseContext(),android.R.layout.simple\u list\u item\u多选,android\u版本);
/**将阵列适配器设置为listview*/
setListAdapter(适配器);
返回super.onCreateView(充气机、容器、savedInstanceState);
}
@凌驾
public void onStart(){
super.onStart();
/**设置listview的multiselect选择模式*/
getListView().setChoiceMode(ListView.CHOICE\u MODE\u SINGLE);
}

为ListviewAdapter创建自定义布局,而不是使用默认设置,请参阅下面的链接以了解更多信息[Link]()

一个简单的方法是将android.R.Layout.simple\u list\u item\u多选项中的代码复制到您自己的布局资源中并进行自定义

在Android-v22中,此代码是:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeightSmall"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" />

然后,您可以将自己的自定义资源传递给ArrayAdapter构造函数:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), R.layout.my_simple_list_item_multiple_choice, android_versions);
ArrayAdapter=new ArrayAdapter(getActivity().getBaseContext(),R.layout.my_simple_list_item_多选,android_版本);

您可以在自定义视图中使用baseadapter,这是在自定义视图中更改颜色的简单方法


参考示例:

有关您的要求,您应该更改自定义适配器而不是默认适配器:在这里您可以找到一些帮助:

public class CustomUsersAdapter extends ArrayAdapter<User> {
public CustomUsersAdapter(Context context, ArrayList<User> users) {
    super(context, 0, users);
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    User user = getItem(position);    
    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) {
       convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false);
    }
    // Lookup view for data population
    TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
    // Populate the data into the template view using the data object
    tvName.setText(user.name);
    tvName.setTextColor(Color.parseColor("#FFFFFF"));
    // Return the completed view to render on screen
    return convertView;
}
}
公共类CustomUsersAdapter扩展了ArrayAdapter{
公共CustomUsersAdapter(上下文,ArrayList用户){
超级(上下文,0,用户);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//获取此职位的数据项
用户=获取项目(位置);
//检查是否正在重用现有视图,否则会膨胀视图
if(convertView==null){
convertView=LayoutInflater.from(getContext()).flate(R.layout.item_user,parent,false);
}
//数据填充的查找视图
TextView-tvName=(TextView)convertView.findViewById(R.id.tvName);
//使用数据对象将数据填充到模板视图中
tvName.setText(user.name);
tvName.setTextColor(Color.parseColor(#FFFFFF”);
//返回要在屏幕上渲染的已完成视图
返回视图;
}
}
您需要一个在数组中用作数据类型的模型

public class User {
public String name;

public User(String name) {
    this.name = name;
}

public static ArrayList<User> getUsers() {
    ArrayList<User> users = new ArrayList<User>();
    users.add(new User("Jelly Bean"));
    users.add(new User("IceCream Sandwich"));
    users.add(new User("HoneyComb"));
    users.add(new User("Ginger Bread"));
    return users;
}
}
公共类用户{
公共字符串名称;
公共用户(字符串名称){
this.name=名称;
}
公共静态ArrayList getUsers(){
ArrayList用户=新建ArrayList();
添加(新用户(“果冻豆”);
添加(新用户(“冰淇淋三明治”);
添加(新用户(“蜂窝”);
添加(新用户(“姜饼”);
返回用户;
}
}
在片段类中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

ArrayList<User> arrayOfUsers = User.getUsers();
    // Create the adapter to convert the array to views
    CustomUsersAdapter adapter = new CustomUsersAdapter(getActivity(), arrayOfUsers);
    // Attach the adapter to a ListView
    ListView listView = (ListView) getActivity().findViewById(R.id.lvUsers);
    listView.setAdapter(adapter);

return super.onCreateView(inflater, container, savedInstanceState);
}
@覆盖
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
ArrayList arrayOfUsers=User.getUsers();
//创建适配器以将阵列转换为视图
CustomUsersAdapter=新的CustomUsersAdapter(getActivity(),ArrayFuser);
//将适配器连接到ListView
ListView ListView=(ListView)getActivity().findViewById(R.id.lvUsers);
setAdapter(适配器);
返回super.onCreateView(充气机、容器、savedInstanceState);
}

现在,您可以在Java或XML“item\u user”中动态更改背景颜色或文本颜色了。

您必须在styles.XML中添加以下代码

<style name="AppTheme" parent="AppBaseTheme">

  <item name="android:itemBackground">@android:color/holo_green_dark</item>

</style>

@安卓:彩色/全息绿/深色

还有其他更简单的方法吗..该文档不包含更改文本颜色的代码Okie,,,检查此方法:-()。非常简单的教程,但您必须做一个更改:-默认情况下使用布局创建您自己的布局。尝试了解无需创建自定义adpater只需创建布局。R.Layout.ownlayout在自己的布局中-只需定义一个文本视图,如下所示:-(现在,通过我上面给出的代码,我正在用tab1.setTableListener(新的CustomTableListener(这个“android”,Contacts.class))之类的选项卡调用这个类;如何调用您的类