更改xml字符串数组中项目的字体颜色

时间:2015-05-26 19:40:09

标签: android xml android-popupwindow

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

字符串数组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);
        }
    });

1 个答案:

答案 0 :(得分:1)

对于其他读这篇文章的人:感谢@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));
相关问题