我试图在弹出窗口中显示一个微调器。我希望每个下拉项的一部分文本是绿色的。更具体地说,一个选项看起来像:
“交易类型:支付现金,见面”。我希望“交易类型”字样为绿色,其余字样为黑色。
我尝试了字体颜色属性,但这不起作用。

字符串数组 xml 文件的代码:

 <?xml version="1.0" encoding="utf-8"?>
<resources>
   <string-array name="payment_list">
    <item><font color="#00ff00">Transaction Type: </font>Pay Cash, Meet Up</item>
    <item><font color="#00ff00">Transaction Type: </font>Credit Card, Meet Up</item>
    <item><font color="#00ff00">Transaction Type: </font>Credit Card, Ship to Me</item>
</string-array>

弹出窗口的代码:
final Button makeoffer = (Button) view.findViewById(R.id.make_offer);
    makeoffer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View popupView = layoutInflater.inflate(R.layout.offer_popup, null);
            final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            EditText mEditText = (EditText) popupView.findViewById(R.id.payment_field);
            popupView.clearFocus();
            mEditText.requestFocus();
            popupWindow.setFocusable(true);
            popupWindow.update();

            final Spinner spinner1 = (Spinner) popupView.findViewById(R.id.transaction_spinner);
            spinner1.setAdapter(new ArrayAdapter<CharSequence>(getActivity(), R.layout.support_simple_spinner_dropdown_item,getActivity().getResources().getTextArray(R.array.payment_list)));

            Button btnDismiss = (Button)popupView.findViewById(R.id.cancel2);
            btnDismiss.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow.dismiss();
                }
            });
            popupWindow.showAtLocation(popupView, Gravity.NO_GRAVITY, 0, 0);
        }
    });

最佳答案

对于其他阅读本文的人:感谢@SirGregg 为我指明了正确的方向。
我做的第一件事是将 CDATA 块添加到我的字符串数组 XML 文件中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string-array name="payment_list">
    <item><![CDATA[<font color="#00ff00">Transaction Type: </font>]]>Pay Cash, Meet Up</item>
    <item><![CDATA[<font color="#00ff00">Transaction Type: </font>]]>Credit Card, Meet Up</item>
    <item><![CDATA[<font color="#00ff00">Transaction Type: </font>]]>Credit Card, Ship to Me</item>
  </string-array>
</resources>

然后,我回到添加微调器的代码并将其更改为:
final Spinner spinner1 = (Spinner) popupView.findViewById(R.id.transaction_spinner);
String[] array = getActivity().getResources().getStringArray(R.array.payment_list);
Spanned[] spannedStrings = new Spanned[3];
for(int i=0; i<array.length; i++){
                spannedStrings[i] = Html.fromHtml(array[i]);
            }
spinner1.setAdapter(new ArrayAdapter<CharSequence>(getActivity(), R.layout.support_simple_spinner_dropdown_item,spannedStrings));

关于android - 更改 xml 字符串数组中项目的字体颜色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30467449/

10-09 12:32