Android的常用控件

在這裏我將介紹如下幾種基本的也是在編程中最常見的幾種控件:
文本類型控件:TextView(負責展示文本,非編輯) ,EditText(可編輯文本)
按鈕類控件:Button 按鈕 ,ImageButton 圖片按鈕 ,RadioButton與RadioGroup 單選按鈕 ,CheckBox 複選按鈕
圖片控件:ImageView 負責顯示圖片
進度條控件 :ProgressBar 進度條


一,文本類控件
1.TextView
TextView是android控件中的最基本的控件,用於展示文字,它是不可編輯的,只能通過初始化或者在代碼中修改。

<TextView
        android:layout_width="wrap_content"//用於編輯文本框的長度
        android:layout_height="wrap_content"//用於編輯文本框的高度 />

wrap_content:自動適配寬度(高)
match_parent:滿屏寬度(高)

android:id = "@+id/xxx"  @+id/xxx表示新增控件命名爲xxx
android:text="@string/hello_world" //兩種方式,直接具體文本或者引用values下面的string.xml裏面的元素
//字體大小
android:textSize="24sp"  //以sp爲單位

//字體顏色
android:textColor="#0000FF"  //RGB顏色

//字體格式
android:textStyle="normal"  //normal,bold,italic分別爲正常,加粗以及斜體,默認爲normal

//文本顯示位置
android:gravity="center"  //來指定文字的對齊方式,可選值有 top、bottom、left、right、center 等

//是否只在一行內顯示全部內容
android:singleLine="true"  //true或者false,默認爲false

TextView的顯示效果
這裏寫圖片描述
2.EditText
EditText是可以與用戶進行交互的,用法也與TextView相類似。
EditText的幾種基本屬性:

<EditText
        android:layout_width="match_parent"//寬度
        android:layout_height="wrap_content"//長度
        android:id="@+id/xxx"//控件id
        android:hint="這是提示屬性"//提示文本內容
        android:textSize="20sp"//字體的大小
        android:textColor="@color/colorPrimary"//字體的顏色
        android:maxLines="2"//設置最大限度的行數
        />

其實EditText與TextView這兩個其實很相似,一個可以編輯,一個不可以編輯。Android的一些控件其實用法很相似,只要先設置控件的長度與高度,再加上一個特有的id,然後再加上一些自有的屬性就可以使用了。

二,按鈕類控件
1.Button
button是程序用於與用戶交互的一個重要的控件,用戶可以通過單擊 Button 來觸發一系列事件,然後爲 Button 註冊監聽器,來實現 Button 的監聽事件。

<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn1"
        android:text="BUTTON"/>

Button其實與上面介紹的控件其實都差不多,不過它可以被用戶所點擊,所以我們經常在Button控件上添加監聽事件來完成用戶所要指定的操作,這一點是非常重要的。

public class MainActivity extends Activity {
    private EditText edittext;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edittext=(EditText) findViewById(R.id.edit_text);
        button = (Button) findViewById(R.id.button);
        //爲button按鈕註冊監聽器,並通過匿名內部類實現
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            //點擊Button會改變edittext的文字爲"點擊了Button"
            edittext.setText("點擊了Button");
            }
        }); 
    }
}

2.按鈕類控件ImageButton
ImageButton和Button類似,是一個按鈕,ImageButton可以實現我們任何想要的圖片按鈕的效果,比如我們租一個下載的按鈕等等。它要比button實現的要好看,並且體驗要好很多, 不過它是以圖片作爲背景,沒有文字。利用屬性android:src=”圖片位置”來設置圖片背景。

<ImageButton
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:src="@mipmap/ic_launcher"//設置圖片背景來源
    />

關於ImageButton它特有的屬性,它可以設置圖片的參數,它可以通過android:scaleType來設置

android:scaleType=”center” 在視圖中心顯示圖片,並且不縮放圖片
android:scaleType=”centercrop” 按比例縮放圖片,使得圖片長 (寬)的大於等於視圖的相應維度
android:scaleType=”centerinside” 按比例縮放圖片,使得圖片長 (寬)的小於等於視圖的相應維度
android:scaleType=”fitcenter” 按比例縮放圖片到視圖的最小邊,居中顯示
android:scaleType=”fitend” 按比例縮放圖片到視圖的最小邊,顯示在視圖的下部分位置
android:scaleType=”fitstart” 把圖片按比例擴大/縮小到視圖的最小邊,顯示在視圖的上部分位置
android:scaleType=”matrix” 用矩陣來繪製
我們可以將任何圖片設置成按鈕
這裏寫圖片描述

3.RadioButton與RadioGroup
它們兩個需要的是兩個相互配合使用,RadioButton是單選按鈕,而RadioGroup爲我們提供了RadioButton單選按鈕的容器。

 <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"/>
    </RadioGroup>

用RadioGroup將RadioButton包裹起來就可實現單選效果。
這裏寫圖片描述

4.CheckBox 複選按鈕
CheckBox是一個可用與多選的複選按鈕

  <CheckBox
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" 
      android:text="1"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3"/>

其在程序中
這裏寫圖片描述

三,圖片控件
ImageView
ImageView在界面用於顯示圖片

<ImageView
      android:layout_width="100dp"
      android:layout_height="100dp" 
      android:src="@mipmap/tongxun"/>

其用法與ImageButton的用法相類似,可以參照它使用此控件
這裏寫圖片描述

四,進度條控件
ProgressBar 進度條
ProgressBar 用於在界面上顯示一個進度條,表示我們的程序正在加載一些數據,運行程序,會看到屏幕中有一個圓形進度條正在旋轉。

 <ProgressBar
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     //默認是圓形進度條,可以知道樣式設置爲水平進度條
     style="?android:attr/progressBarStyleHorizontal"
     //設置進度條的最大值
     android:max="100"/>

進度條的顯示
這裏寫圖片描述

以上就是我所介紹的各種基本控件,當然了Android中的控件可不包括這幾種,其他的我就不一一介紹了,畢竟能力有限吧。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章