android arrayadapter改变字体颜色,android-如何更改简单列表项的文本颜色

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值