【问题标题】:Is it possible to change specific text color inside a string name stored in strings.xml?是否可以更改存储在 strings.xml 中的字符串名称中的特定文本颜色?
【发布时间】:2019-08-10 13:20:23
【问题描述】:

奇怪的请求,我知道,但出于特殊原因,是否可以更改 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();
}}

【问题讨论】:

  • 那么这段文字出现在哪里?在TextView 中所有文本都应该是蓝色的,还是在TextView 中混合颜色?还是别的地方?
  • 抱歉,没有说明清楚,当用户单击帮助按钮时,文本会出现在 DialogFragment 中。用代码更新帖子。
  • 看看this
  • 真棒你摇滚!有一个很棒的一周朋友。也发现了这个developer.android.com/guide/topics/resources/…

标签: android xml string


【解决方案1】:

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

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

    <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");
  }
}

【讨论】:

    【解决方案2】:

    是的。

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

    这是一个使用示例。

    Example

    重要的是&lt;font color='#303f9f'&gt;WORDS&lt;/font&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      • 2015-09-23
      • 2012-04-11
      • 1970-01-01
      • 2011-06-05
      • 2013-04-17
      • 1970-01-01
      相关资源
      最近更新 更多