android-如何更改簡單清單項的文本顔色
我有一個ListActivity,并且正在顯示一個清單:
setListAdapter(new ArrayAdapter(getApplicationContext(),
android.R.layout.simple_list_item_single_choice, mStringList));
預設情況下,清單項的文本顔色為白色,我想将清單中文本視圖的文本顔色更改為黑色。
我該怎麼辦?
12個解決方案
84 votes
另一種最簡單的方法是建立一個布局檔案,該布局檔案包含所需的文本視圖以及您喜歡的textSize,textStyle,color等,然後将其與ArrayAdapter一起使用。
例如 mytextview.xml
android:id="@+id/tv"
android:textColor="@color/font_content"
android:padding="5sp"
android:layout_width="fill_parent"
android:background="@drawable/rectgrad"
android:singleLine="true"
android:gravity="center"
android:layout_height="fill_parent"/>
然後像往常一樣将它與ArrayAdapter一起使用
ListView lst = new ListView(context);
String[] arr = {"Item 1","Item 2"};
ArrayAdapter ad = new ArrayAdapter(context,R.layout.mytextview,arr);
lst.setAdapter(ad);
這樣,您将無需為其建立自定義擴充卡。
dharmin007 answered 2020-08-09T20:18:22Z
47 votes
我意識到這個問題有點老了,但是這裡缺少一個非常簡單的解決方案。您無需建立自定義ListView甚至是自定義布局。
隻需建立ArrayAdapter的匿名子類并覆寫getView()。 讓super.getView()處理所有繁重的工作。 由于simple_list_item_1僅為文本視圖,是以您可以對其進行自定義(例如,設定textColor),然後将其傳回。
這是我的一個應用程式中的一個示例。 我正在顯示最近位置的清單,我希望所有出現的“目前位置”為藍色,其餘為白色。
ListView listView = (ListView) this.findViewById(R.id.listView);
listView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, MobileMuni.getBookmarkStore().getRecentLocations()) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getView(position, convertView, parent);
String currentLocation = RouteFinderBookmarksActivity.this.getResources().getString(R.string.Current_Location);
int textColor = textView.getText().toString().equals(currentLocation) ? R.color.holo_blue : R.color.text_color_btn_holo_dark;
textView.setTextColor(RouteFinderBookmarksActivity.this.getResources().getColor(textColor));
return textView;
}
});
Cam Saul answered 2020-08-09T20:18:51Z
35 votes
您隻需覆寫ArrayAdapter的getView方法
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(),
android.R.layout.simple_list_item_1, mStringList) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setTextColor(Color.BLACK);
return view;
}
};
chip answered 2020-08-09T20:19:11Z
7 votes
無需建立任何額外操作的最簡單方法就是修改簡單清單TextView:
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, android.R.id.text1, strings) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getView(position, convertView, parent);
textView.setTextColor({YourColorHere});
return textView;
}
};
mattfred answered 2020-08-09T20:19:31Z
4 votes
用簡單的詞“您不能通過簡單的setListAdapter做到”。 您必須使用自定義清單視圖來自由更改文本顔色或任何其他視圖
對于自定義清單視圖,您可以使用此連結
Ayudh answered 2020-08-09T20:19:55Z
3 votes
您可以使用setTextColor(int)方法或添加樣式來更改文本顔色。
#2F2E86
bold
10dip
Ajay Singh answered 2020-08-09T20:20:15Z
2 votes
我也面臨這種情況。 您可以處理上述方法。 但如果通過則很簡單simple_dropdown_item_1line作為資源。 預設情況下,它将是黑色文本。
這是我在某種情況下實作的代碼。
ArrayAdapter arrAdapter =
new ArrayAdapter(context,android.R.layout.simple_dropdown_item_1line,countryList);
listView.setAdapter(arrAdapter);
您的代碼如下:
setListAdapter(new ArrayAdapter(getApplicationContext(),
android.R.layout.simple_dropdown_item_1line, mStringList));
實際上android.R.layout.simple_dropdown_item_1line用于下拉菜單。 我敦促您一次檢視此布局内容。 它隻是簡單的textview。 就這樣。 它的屬性不會影響您的任務。 我檢查了它工作正常。
Praveen Kumar Sugumaran answered 2020-08-09T20:20:49Z
1 votes
嘗試使用以下代碼代替android.r.layout.simple_list_item_single_choice建立自己的布局:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:gravity="center_vertical"
android:padding="5dip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black"
android:textStyle="bold">
user2797839 answered 2020-08-09T20:21:09Z
1 votes
如果您想保留所有樣式但隻更改一些細節,則可以使用Android上定義的預設樣式并更改所需的樣式
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:textColor="@android:color/background_light"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
然後使用以下指令設定擴充卡:
setListAdapter(new ArrayAdapter(getApplicationContext(),
R.layout.list_item_custom, mStringList));
來源:[https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/layout/simple_list_item_activated_1.xml]
Bruno Peres answered 2020-08-09T20:21:37Z
1 votes
如果要建立擴充擴充卡的類,則可以使用父變量擷取上下文。
public class MyAdapter extends ArrayAdapter {
private Context context;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
context = parent.getContext();
context.getResources().getColor(R.color.red);
return convertView;
}
}
您可以使用RecyclerView.Adapter進行相同的操作,但是将使用onCreateViewHolder()代替getview()。
Beto Caldas answered 2020-08-09T20:22:02Z
0 votes
在res / values中建立一個xml檔案并複制以下代碼
#000000
并在清單中的活動中指定樣式,如下所示
android:theme="@style/BlackText"
Anu answered 2020-08-09T20:22:26Z
-1 votes
試試這個代碼...
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffff00">
android:id="@+id/android:list"
android:layout_marginTop="2px"
android:layout_marginLeft="2px"
android:layout_marginRight="2px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/shape_1"
android:listSelector="@drawable/shape_3"
android:textColor="#ffff00"
android:layout_marginBottom="44px" />
kannappan answered 2020-08-09T20:22:46Z