Android中string.xml的常识和相关内容

1.占位符

2.特殊符号内容的转义

===================

以上是我本次备忘的关于Android中string.xml文件中获取特定字符串内容使用的常见问题。


1.占位符:

在一串字符串中希望部分内容是可变的,并且通过传值的方式去更改内容。

举例:

文案信息:很高兴可以收到你赠送的鲜花200金币

其中“鲜花”和“200金币”是通过后台请求Json返回的,根据不同返回内容进行替换。

常见占位符:

%1$d 1代表第一个参数,d代表整数

%2$s 2代表第二个参数,s代表字符串

%3$.2f 3代表第三个参数,.2f代表两位小数

除此之外: f代表浮点数,

解决方案:

1.在string.xml中保存文案信息

2.在activity中通过getString(resId,formatArgs)方法进行获取,后面可以加多个参数

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">StringDemo</string>
    <string name="hello_world">Hello world!</string>
    
    <string name="code41_string_demo_value">很高兴可以收到你赠送的%1$s和%2$d金币。</string>

</resources>
MainActivity.java

package com.code41.demo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView demoTextView;// TextView for test string.xml
	private Handler mHandler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		demoTextView = (TextView) findViewById(R.id.text_view_string_demo);

		// 大部分应用都是网络请求应用,所以要养成异步赋值的好习惯
		post(new Runnable() {

			@Override
			public void run() {
				String stringDemoValue = getString(R.string.code41_string_demo_value, "鲜花", 200);
				demoTextView.setText(stringDemoValue);
			}
		});
	}

	private void post(Runnable runnable) {
		if (isFinishing() || null == mHandler) {
			return;
		}
		mHandler.post(runnable);
	}
}
activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f8f8f8" >

    <TextView
        android:id="@+id/text_view_string_demo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="#666666"
        android:textSize="20sp" />

</RelativeLayout>

效果截图:




2.转义字符内容:

在程序开发中,Html.fromHtml是个很好用的方法,而且很多文案中会要求在同一个textView中使用多种颜色或者多种样式。

举例:

文案信息:很高兴可以收到你赠送的鲜花200金币。(#eb6067 完成部分字体的颜色变化和加粗设置

解决办法:

取值的过程与之前的一致,在string.xml中设置的值需要加入html的样式,同时取出后通过Html.fromHtml的方法进行转换样式。

使用以下方法进行转义,但是记住获取到的字符串含有html标签,需要使用Html.fromHtml方法转换成带有样式的Spanned设置给TextView才可以。

<Data><![CDATA[<font color="#eb6067"><b>%1$s</b></font>和<font color="#eb6067"><b>%2$d</b></font>金币]]></Data>

代码:

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">StringDemo</string>
    <string name="hello_world">Hello world!</string>
    
    <string name="code41_string_demo_value">很高兴可以收到你赠送的%1$s和%2$d金币。</string>
    <string name="code41_style_string_demo_value">很高兴可以收到你赠送的<Data><![CDATA[<font color="#eb6067"><b>%1$s</b></font>和<font color="#eb6067"><b>%2$d</b></font>金币]]></Data>。</string>

</resources>
MainActivity.java

package com.code41.demo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.text.Spannable;
import android.text.Spanned;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView demoTextView;// TextView for test string.xml
	private TextView styleDemoTextView;// TextView for test string.xml
	private Handler mHandler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		demoTextView = (TextView) findViewById(R.id.text_view_string_demo);
		styleDemoTextView = (TextView) findViewById(R.id.text_view_style_string_demo);

		// 大部分应用都是网络请求应用,所以要养成异步赋值的好习惯
		post(new Runnable() {

			@Override
			public void run() {
				// 设置无样式的字符串内容
				String stringDemoValue = getString(R.string.code41_string_demo_value, "鲜花", 200);
				demoTextView.setText(stringDemoValue);
				// 设置HTML样式的字符串内容
				String styleStringDemoValue = getString(R.string.code41_style_string_demo_value, "鲜花", 200);
				Spanned styleSpanned = Html.fromHtml(styleStringDemoValue);
				styleDemoTextView.setText(styleSpanned);
			}
		});
	}

	private void post(Runnable runnable) {
		if (isFinishing() || null == mHandler) {
			return;
		}
		mHandler.post(runnable);
	}
}
activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f8f8f8" >

    <TextView
        android:id="@+id/text_view_string_demo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="#666666"
        android:textSize="20sp" />
    
    <TextView
        android:id="@+id/text_view_style_string_demo"
        android:layout_below="@+id/text_view_string_demo"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#666666"
        android:textSize="20sp" />
    

</RelativeLayout>

截图:


代码在这里










發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章