是否可以在strings.xml中存储的字符串名称内更改特定的文本颜色?

时间:2019-03-19 22:13:46

标签: android xml string

我知道奇怪的要求,但是出于特殊原因,是否可以更改strings.xml字符串中文本的颜色?例如,我希望问题文本为特定颜色。当用户单击帮助按钮时,文本将出现在DialogFragment中。

<string name="vendor_alert_text">Question 1? Answer. Questions 2? Answer. Question 3? Answer</string>

public class CustomerHelpDialog extends AppCompatDialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity());
    alertBuilder.setTitle(R.string.help_dialog_title)
            .setMessage(R.string.customer_alert_text)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                //Empty to close the dialog
                }
            });

    return alertBuilder.create();
}}

enter image description here

2 个答案:

答案 0 :(得分:1)

您不应该尝试在strings.xml中更改文本颜色,只需对TextView使用android:textColor标记即可显示文本。

使用DialogFragment,您可以使用任何自定义布局,在其中可以为TextViews设置所需的任何颜色:

    <string name="dialog_1">Dialog 1</string>
<string name="dialog_2">Dialog 2</string>
<string name="message_text">Text of your message</string>
<string name="yes">Yes</string>
<string name="no">No</string>
<string name="maybe">Maybe</string> 

*

 <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="20dp"
    android:textColor="@color/colorPrimary" //ANY COLOUR YOU WANT
    android:text="@string/message_text"
    android:textAppearance="?android:attr/textAppearanceLarge">
</TextView>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <Button
        android:id="@+id/btnYes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="@string/yes">
    </Button>
    <Button
        android:id="@+id/btnNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="@string/no">
    </Button>
    <Button
        android:id="@+id/btnMaybe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="@string/maybe">
    </Button>
</LinearLayout>

    public class Dialog1 extends DialogFragment implements OnClickListener {

  final String LOG_TAG = "myLogs";

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    getDialog().setTitle("Title!");
    View v = inflater.inflate(R.layout.dialog1, null);
    v.findViewById(R.id.btnYes).setOnClickListener(this);
    v.findViewById(R.id.btnNo).setOnClickListener(this);
    v.findViewById(R.id.btnMaybe).setOnClickListener(this);
    return v;
  }

  public void onClick(View v) {
    Log.d(LOG_TAG, "Dialog 1: " + ((Button) v).getText());
    dismiss();
  }

  public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    Log.d(LOG_TAG, "Dialog 1: onDismiss");
  }

  public void onCancel(DialogInterface dialog) {
    super.onCancel(dialog);
    Log.d(LOG_TAG, "Dialog 1: onCancel");
  }
}

答案 1 :(得分:0)

是的。

<string name="concordoTermosEPolitica">Concordo com os <font color='#303f9f'>Termos de Uso</font> e a <font color='#303f9f'>Política de Privacidade.</font></string>

这是一个示例。

Example

重要的是<font color='#303f9f'>WORDS</font>