1、activity_hello_world.xml文件内容如下

http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent" ><TextView            android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world"        tools:context=".HelloWorld" /><Button      android:id="@+id/toRed"     android:layout_width="80sp"     android:layout_gravity="center"     android:layout_height="wrap_content"     android:text="@string/bToRed" /><Button      android:id="@+id/toBlue"     android:layout_width="80sp"     android:layout_gravity="center"     android:layout_height="wrap_content"     android:text="@string/bToBlue" />


android:id 只控件的id,后面会用到这个id

android:layout_width 控件的宽度

android:layout_height 控件的高度

android:text 控件里的文字

tips:

android:layout_width和android:width的区别。前者指控件的宽度,后者指控件内的“内容”的宽度

2、onCreate方法

public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_hello_world);        text = (TextView) findViewById(R.id.text);        bToRed = (Button) findViewById(R.id.toRed);        bToRed.setTextColor(Color.RED);       // 设置Button里面文字的颜色        bToBlue = (Button) findViewById(R.id.toBlue);        bToBlue.setTextColor(Color.BLUE);    }


这里可以通过findViewById方法获得前面设置过id的控件。

大家运行之后会发现,无论你怎么点击Button就是没有反应,这是因为我们还没有给Button添加事件。

更多相关文章

  1. android学习之textView布局文件
  2. android 骁龙相机预览宽度方向全屏
  3. android2.x中android:layout_marginRight不起作用的解决办法
  4. 时钟控件AnalogClock和DigitalClock
  5. [置顶] android对字段的验证
  6. 线性布局的常用属性
  7. Android:RelativeLayout布局的相关属性介绍
  8. 安卓基本常用控件: View
  9. Android回顾--(九) ListView和自定义适配器

随机推荐

  1. 深度解析C++的函数模板与类模板
  2. 如何快速生成数据的文本路径呢?C++实现文
  3. 如何使用LINQ、Lambda 表达式 、委托快速
  4. C#中将DataGridView中的数据导入到Csv文
  5. C#学习记录:编写高质量代码改善整理建议9-
  6. c# 如何生成自定义图片?c# 生成自定义图片
  7. C#学习记录:编写高质量代码改善整理建议4-
  8. c++中string&char *&char[]之间如何转换(
  9. 基于C的文件操作 (FILE*、fstream、window
  10. C#学习记录:编写高质量代码改善整理建议1-