Android学习--TextView和Button

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添加事件。

 

你可能感兴趣的:(Android学习--TextView和Button)