这个方法,就是一个占位符的使用。
很多java代码都用这样的用法,android中也有这个用法。
工具/原料
- 编译工具:Eclipse
方法/步骤
-
首先展示代码结构
步骤阅读
-
页面类
MainActivity
步骤阅读
-
MainActivity
代码如下:
public class MainActivity extends ActionBarActivity {
TextView test_fonts; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); test_fonts = (TextView)findViewById(R.id.test_fonts); String test_text = getString(R.string.test_fonts); test_text = String.format(test_text, "我就是测试文字!!!!"); test_fonts.setText(test_text); }
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
步骤阅读
-
页面布局文件
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.android_test.MainActivity" >
<TextView android:id="@+id/test_fonts" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/test_fonts" />
</RelativeLayout>
步骤阅读
-
要调用的string.xml
步骤阅读
-
string.xml的类容
步骤阅读
-
string.xml的代码
<?xml version="1.0" encoding="utf-8"?><resources>
<string name="app_name">android_test</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string>
<string name="test_fonts">用来测试的文字:%1$s!</string> </resources>
-
最后还是把最关键的代码提出来说一下
步骤阅读
-
项目代码执行生成的页面如下:
%[index]$[type]这个是占位符的编写格式
例如:用来测试的文字:%1$s!
这样大家应该看的懂了吧
步骤阅读
END
注意事项
- 本步骤需要一定代码基础
- 程序代码以代码片段的形式展示
本文来自投稿,不代表幸运快三立场,转载请注明出处:http://www.morucat.com/digital/8011.html