简体   繁体   中英

Set textView color programmatically

this code shows the text:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);
    if (mItem != null) {
        TextView txtBig = (TextView) rootView.findViewById(R.id.item_detail2);
        txtBig.setText(mItem.content);
    }

    return rootView;
}

but when I use:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);
        if (mItem != null) {
            TextView txtBig = (TextView) rootView.findViewById(R.id.item_detail2);
            txtBig.setText(mItem.content);
            txtBig.setTextColor(Color.parseColor("#ff0000"));
        }
        return rootView;
    }

there is no text on the screen. I've tried txtBig.setTextColor(0xff000000); too , but no luck.

Can you please try below code:

txtBig.setTextColor(getResources().getColor(R.color.yellow));

You have to add yellow color in color.xml in Resources -> Value Directory.

<color name="yellow">#FFFF00</color>

Hope it will help you.

It seems like, the color of your text and the background where the text is actually rendered are same. Can you change the color of the text and see if the text is visible?

txtBig.setTextColor(Color.parseColor("#ffffff"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM