Android学习--TextView和Button
16lz
2021-12-04
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布局文件
- android 骁龙相机预览宽度方向全屏
- android2.x中android:layout_marginRight不起作用的解决办法
- 时钟控件AnalogClock和DigitalClock
- [置顶] android对字段的验证
- 线性布局的常用属性
- Android:RelativeLayout布局的相关属性介绍
- 安卓基本常用控件: View
- Android回顾--(九) ListView和自定义适配器