今天我們來和大家一起認(rèn)識Android幾個最基本的控件,分別是顯示文本的TextView、用于輸入額EditText、用于點擊的Button和顯示圖像的ImageView,那為了更好地呈現(xiàn)這幾個控件了,我們先認(rèn)識一下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"
>
上面我沒放圖片,考慮到各位那邊不一定有那張圖片,你們可以添加自己圖片測試
六、總結(jié)
Android的控件都繼承于View,View都有一下幾個常用屬性,也都可以給View設(shè)置點擊事件。
- 控件的寬
android:layout_width="match_parent"
- 控件的高
android:layout_height="40dp"
- 背景顏色
android:background="#ff0000"
- id
android:id="@+id/iv_g1"