配置一个简单的程序
主要通过src
|-java
和 res
|-layout
|-main.xml
以及 |-values
|-strings.xml 来完成
第一个在strings.xml没有参与的情况下通过ID搭配配置文件
在main.xml下配置textview和button
<TextView
android:id="@+id/mytext" -->要调取的ID
android:layout_width="fill_parent" ----》宽度
android:layout_height="wrap_content" ----》高度
/>
<Button
android:id="@+id/mybut" -->按钮ID
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
然后在java程序中来调取
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView text = (TextView) super.findViewById(R.id.mytext);//取得TextView组件
text.setText(“中国人”);//设置显示文字
Button but= (Button) super.findViewById(R.id.mybut);//取得按钮组件
but.setText(“按我”);
}
第二种在strings参与的情况下
一时strings参与xml
<TextView
android:id="@+id/mytext" -->要调取的ID
android:layout_width="fill_parent" ----》宽度
android:layout_height="wrap_content" ----》高度
android:text="@strings/text" -->要展示的文本 />
<Button
android:id="@+id/mybut" -->按钮ID
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@strings/msg" />
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//TextView text = (TextView) super.findViewById(R.id.mytext);//取得TextView组件
//text.setText(“中国人”);//设置显示文字
//Button but= (Button) super.findViewById(R.id.mybut);//取得按钮组件
//but.setText(“按我”);
}
二是strings参与java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView text = (TextView) super.findViewById(R.id.mytext);//取得TextView组件
text.setText(R.string.text);//设置显示文字
Button but= (Button) super.findViewById(R.id.mybut);//取得按钮组件
but.setText(R.string.msg);
}
这两种情况都要在strings中添加
<resources>
<string name="hello">Hello World, ZhActivity!</string>
<string name="app_name">Zh</string>
<string name="text">中国人</string>
<string name="msg">按我</string>
</resources>
第三种情况
通过程序动态生成
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
TextView text = new TextView(this);
text.setText(super.getString(R.string.text));//通过strings.xml文件设置文字
Button but = new Button(this);//定义按钮
but.setText(super.getString(R.string.msg));//配置组件文字
layout.addView(text);//向布局管理器之中增加文本组件
layout.addView(but);//想布局管理器之中增加按钮组件
super.setContentView(layout);//设置要使用的布局管理器
}