如何更改ListView项目的颜色

时间:2015-08-31 08:11:59

标签: android listview

我有一个包含硬编码项目的列表,其中bydefault是白色的,我的白色背景几乎看不见。 public class Message扩展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);       
}

5 个答案:

答案 0 :(得分:2)

为ListviewAdapter制作自定义布局而不是使用默认布局,请参阅以下链接以获取更多信息[链接](http://www.vogella.com/tutorials/AndroidListView/article.html

答案 1 :(得分:0)

一种简单的方法是将 android.R.layout.simple_list_item_multiple_choice 中的代码复制到您自己的布局资源中并自定义它。

在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);

答案 2 :(得分:0)

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

参考示例:http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/

答案 3 :(得分:0)

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

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

您需要一个在数组中用作数据类型的模型

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

在你的片段类中:

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

现在,您可以使用Java或XML“item_user”动态更改background或textColor的颜色

答案 4 :(得分:0)

你必须将下面的代码放在styles.xml中

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

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

</style>
相关问题