English 中文(简体)
定 义 从方案角度看待<selector>
原标题:Setting TextView color to a <selector> programmatically

在XML档案中,我有以下选定人:res/color/redeemlist_item_color.xml:

<?xml version="1.0" encoding="utf-8"?>
   <selector xmlns:android="http://schemas.android.com/apk/res/android">

      <item android:state_pressed="true"
            android:color="#FFFFFF" /> <!-- pressed -->

      <item android:state_selected="true"
            android:color="#FFFFFF" /> <!-- focused -->

      <item android:color="#000000" /> <!-- default -->

   </selector>

I also have a TextView in a ListView item layout. When I set android:textColor on this TextView to the above selector in XML, then the color changes correctly when the item is selected. However, I am trying to set this resource programmatically in the following way:

holder.label.setTextColor(R.color.redeemlist_item_color);

这样,肤色就不再改变。 能够以这种方式指派一名选择人到<条码>。

最佳回答

I think you might need to add findViewById or something of that variety


Edit:上述说法不正确,因为根据我的意见,适当的答案是正确的。

setTextColor(getResources().getColorStateList(R.color.redeemlist_item_color));
问题回答

页: 1

I was also struggling with this problem, if you want to have use a state list, you need to declare it in the color resources folder, instead of the drawable folder, and use the setTextColor(getResources().getColorStateList(R.color.redeemlist_item_color)).

You can try:

holder.label.setTextColor(getResources().getColor(R.color.redeemlist_item_color));

而不是:

持有人.label.setTextColor(R.color.redeemlist_item_color);

Rasman is correct. You need to give the TextView an ID, android:id="@+/something". You retrieve a reference to that particular using that ID and findViewById, and then you may set the text color.





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签