3rd-经典UI操作1-更改与显示文字标签
修改部分主要为两方面:
1、res->values->string.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 <string name="app_name">TextView</string> 4 <string name="hello">Welcome!</string> 5 <string name="action_settings">Settings</string> 6 </resources>
解释一下:修改从3,4行。第三行是软件名,第四行是显示的文字
2、res->layout->activity_main.xml
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.hello2.MainActivity" > 10 11 <TextView 12 android:id="@+id/mTextView" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="@string/hello" /> 16 </RelativeLayout>
修改TextView里的内容:
首先加上12行的语句
第15行和第一部分的4中名称相互对应,均为hello;
注意规范,否则容易报错
3、最终效果

知识点学习:
TextView是屏幕中一块用来显示文本的区域,它属于Android包并继承Android.view.View类。它是一个不可编辑的文本框,往往用来在屏幕中显示静态字符串,其功能类似于java语言中swing包的jlabel组件。
接下来从java中修改:
有个前提:在Java类里,找到TextView对象,并且可以控制它,这就需要上述步骤12。在xml中修改后在主程序进行如下修改:
1 package com.example.hello2; 2 3 import android.support.v7.app.ActionBarActivity; 4 import android.os.Bundle; 5 import android.view.Menu; 6 import android.view.MenuItem; 7 import android.widget.TextView; 8 public class MainActivity extends ActionBarActivity { 9 private TextView myTextView; 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 setContentView(R.layout.activity_main); 14 myTextView=(TextView) findViewById(R.id.myTextView); 15 String welcome_mes="欢迎"; 16 myTextView.setText(welcome_mes); 17 } 18 19 @Override 20 public boolean onCreateOptionsMenu(Menu menu) { 21 // Inflate the menu; this adds items to the action bar if it is present. 22 getMenuInflater().inflate(R.menu.main, menu); 23 return true; 24 } 25 26 @Override 27 public boolean onOptionsItemSelected(MenuItem item) { 28 // Handle action bar item clicks here. The action bar will 29 // automatically handle clicks on the Home/Up button, so long 30 // as you specify a parent activity in AndroidManifest.xml. 31 int id = item.getItemId(); 32 if (id == R.id.action_settings) { 33 return true; 34 } 35 return super.onOptionsItemSelected(item); 36 } 37 }
代码中首先导入了Android.app.Activity,android.os.Bundle和android.widget.TextView包。
@Override标示后面定义的方法是从父类或者接口中继承过来的,需要重写。onCreat()是程序执行的入口,包含一个bundle类型的参数,可用于不同activity之间的消息传递。
super.onCreate(savedInstanceState)这条语句不能省略。
setContentView(R.layout.activity_main)这个方法指定了一个activity布局,在R.中定义的。
findViewById(R.id.myTextView)唯一识别一个widget对象
在程序设计完后,屏幕显示改变,因为重新定义了对象的显示属性。

最后补充一下xml中的内容含义:
1、LinearLayout标签定义了布局:
orientation:包括vertical垂直 和 horizontal水平
layout_width:包括fill_parent整个屏幕宽度 match_parent 宽度与父元素相同 wrap_content宽度随期间本身的内容所调整 通过指定数值来设置宽度
layout_height:包括 fill_parent整个屏幕高度 match_parent 高度与父元素相同 wrap_content高度随期间本身的内容所调整 通过指定数值来设置宽度
2、子标签 TextView对象
android:id属性使用@+id声明了一个句柄,定义了该对象的唯一表示myTextView,这个标识的值在R.java中被定义。(xml被编译时,自动添加)

浙公网安备 33010602011771号