2015-08-31 230 views
0

我有一個包含硬編碼項目的列表,默認情況下,白色是幾乎看不到我的whitebackground的顏色。 公共類信息擴展ListFragment {如何更改ListView項目的顏色

/** 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);  
} 

回答

2

使自定義佈局ListviewAdapter而不是使用默認值,請參考以下鏈接更多瞭解[鏈接](http://www.vogella.com/tutorials/AndroidListView/article.html

+0

是否有其他更簡單的方法..這文檔中不包含更改文本顏色的代碼 – Loren

+0

Okie ,,,選中此項: - (http://www.java-samples.com/showtutorial.php?tutorialid=1516)。很簡單的教程,但你必須做一個改變: - 使用默認情況下佈局使你自己的。嘗試理解不需要製作自定義adpater只需佈局.R.layout.OWNLAYOUT – user1986760

+0

在自己的佈局 - 只需定義一個Textview就像下面: - (<?xml version =「1.0」encoding =「UTF-8」?> android:textSize =「#000000」android:ellipsize =「marquee」android:gravity =「left | center_vertical」android:textSize =「16sp」android: layout_height =「match_parent」android:layout_width =「fill_parent」xmlns:android =「http://schemas.android.com/apk/res/android」/>) – user1986760

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

0

你必須把下面的代碼在styles.xml

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

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

</style> 
相關問題