今天我们来和大家一起认识Android几个最基本的控件,分别是显示文本的TextView、用于输入额EditText、用于点击的Button和显示图像的ImageView,那为了更好地呈现这几个控件了,我们先认识一下Android的一种最简单的布局——线性布局
一、线性布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="#0f0"
>
</LinearLayout>
二、文本控件TextView
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是用于显示文字的控件"
/>
三 、输入控件
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="请输入文字"
/>
四、按钮控件Button
<Button
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="这是个按钮"/>
五 图片控件ImageView
<ImageView
android:id="@+id/iv_g1"
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#888"
>
上面我没放图片,考虑到各位那边不一定有那张图片,你们可以添加自己图片测试
六、总结
Android的控件都继承于View,View都有一下几个常用属性,也都可以给View设置点击事件。
- 控件的宽
android:layout_width="match_parent"
- 控件的高
android:layout_height="40dp"
- 背景颜色
android:background="#ff0000"
- id
android:id="@+id/iv_g1"