更改 xml 字符串数组中项目的字体颜色
Changing font color of items in xml string-array
我正在尝试在弹出窗口中显示微调器 window。我希望每个下拉项的部分文本为绿色。更具体地说,一个选项看起来像:
"Transaction Type: Pay Cash, Meet Up"。我希望单词 "Transaction Type" 为绿色,其余单词为黑色。
我尝试了字体颜色属性,但它不起作用。
字符串数组 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>
弹出代码 window:
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));
我正在尝试在弹出窗口中显示微调器 window。我希望每个下拉项的部分文本为绿色。更具体地说,一个选项看起来像: "Transaction Type: Pay Cash, Meet Up"。我希望单词 "Transaction Type" 为绿色,其余单词为黑色。 我尝试了字体颜色属性,但它不起作用。
字符串数组 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>
弹出代码 window:
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));