安卓入门系列-07常用UI控件(长文)
生活随笔
收集整理的這篇文章主要介紹了
安卓入门系列-07常用UI控件(长文)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
常用UI控件
- 簡介
- 這一篇介紹開發中的常用UI控件。
- 布局管理器
- 所有布局管理器都是ViewGroup的子類,都可作為容器類使用。繼承自View,所以也可嵌套。
- 常見的布局之前已經提到了三種,這里不再提,只需要知道,它一般作為控件的容器,利用布局管理器方便布局的開發。
- TextView及其子類
- Textview文本框
- 顯示文本內容的文本區域,不可編輯。
- EditText編輯框
- 顯示文本內容,可編輯修改。
- Button按鈕
- 可點擊的一個按鈕,點擊觸發onclick事件,其實是TextView的點擊實現。在開發中,無論裝飾得多么花哨,很多控件的本質依然是Button。
- RadioButton:單選按鈕,一般和RadioGroup一起使用組成選項組
- CheckBox:復選按鈕
- ToggleButton:狀態開關按鈕
- Switch:開關
- Clock鐘
- TextClock:取代DigitalClock組件,能以24/12 小時制來顯示時間,數字。
- AnalogClock:繼承自View組件,overwrite了View的OnDraw方法,它會在View上繪制模擬時鐘。使用這個控件IDE加底線,表示不建議使用了。
- Chronometer計時器
- 倒計時。
- 代碼測試
- <?xml version="1.0" encoding="utf-8"?><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="com.zc.helloworld.MainActivity"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="這是一個TextView"/><EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="這是一個EditText"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="這是一個Button"/><RadioButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="這是一個RadioButton"/><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="這是一個CheckBox"/><ToggleButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content" /><Switchandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="這是一個Switch"/><TextClockandroid:layout_width="wrap_content"android:layout_height="wrap_content" /><AnalogClockandroid:layout_width="wrap_content"android:layout_height="wrap_content" /><Chronometerandroid:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>
- 演示結果
- Textview文本框
- ImageView及其子類
- 繼承自View,顯示圖片。
- ImageButton圖片按鈕
- 圖片按鈕,可觸發。
- QuickContactBadge關聯聯系人圖片
- 顯示關聯到特定聯系人的圖片。
- ZoomButton圖片縮放按鈕
- 代表”放大”、”縮小”兩個按鈕。(需要代碼控制)
- 代碼測試
- <?xml version="1.0" encoding="utf-8"?><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="com.zc.helloworld.MainActivity"android:orientation="vertical"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/ic_launcher"/><ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/ic_launcher"/><QuickContactBadgeandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/ic_launcher"/><ZoomButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/ic_launcher"/></LinearLayout>
- 演示結果
- AdapterView及子類
- 本身是一個抽象基類,繼承了ViewGroup,它的本質是容器,它無法直接使用,是一種自動適配的容器。
- Listview列表視圖
- 垂直顯示列表內容的控件。
- Adapter適配器
- 用內容填充列表視圖。
- AutoCompleteTextView自動完成文本框
- AutoCompleteTextView由Editext派生,實際上它也是一個編輯框,但它比普通編輯框多了一個功能:當用戶輸入一定字符之后,自動完成文本框會顯示一個下拉菜單,供用戶從中選擇。
- GridView網格視圖
- 類似于ListView,但是多列。
- ExpandableListView可展開的列表組件
- 列表項分組。
- 這一部分均需要適配器,后面博客提到,就不演示了。
- ProgressBar及其子類
- ProgressBar進度條
- 進度條通常用于向用戶顯示一個百分比。進度條可以動態地顯示進度,避免用戶死等的尷尬狀態。用顏色填充表明進度。
- SeekBar拖動條
- 拖動條和進度條非常相似,通過滑塊的位置來標識數值,允許用戶拖動滑塊來改變值,因此拖動條通常用于對系統的某種數值進行調節,亮度調節的底層就是它。
- RatingBar星級評分條
- 允許用戶通過拖動來改變進度,不過RatingBar通過星星來表示進度。
- 代碼測試
- <?xml version="1.0" encoding="utf-8"?><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="com.zc.helloworld.MainActivity"android:orientation="vertical"><ProgressBarandroid:layout_width="match_parent"android:layout_height="wrap_content" /><SeekBarandroid:layout_width="match_parent"android:layout_height="wrap_content" /><RatingBarandroid:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>
- 演示結果
- ProgressBar進度條
- 對話框
- 以下對話框均需要具體代碼實現邏輯,后面介紹,這里只演示效果。
- AlertDialog彈出對話框
- 功能最豐富、實際應用最廣的對話框
- ProgressDialog進度條對話框
- 進度條對話框、這個對話框只是對進度條的包裝
- DatePickerDialog日期選擇對話框
- 日期選擇對話框,這個對話框只是對DatePicker的包裝
- TimePickerDialog時間選擇對話框
- 時間選擇對話框,這個對話框只是對TimePicker的包裝。
- 代碼測試
- 一般代碼控制彈出,演示需要,我讓它顯示出來了。
- <?xml version="1.0" encoding="utf-8"?><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="com.zc.helloworld.MainActivity"android:orientation="vertical"><ProgressBarandroid:layout_width="wrap_content"android:layout_height="wrap_content" /><TimePickerandroid:layout_width="wrap_content"android:layout_height="wrap_content"></TimePicker><TimePickerandroid:layout_width="wrap_content"android:layout_height="wrap_content"></TimePicker></LinearLayout>
- 演示結果
- 菜單(Menu)
- Android提供了兩種創建菜單的方式,一種是代碼創建,一種是使用xml資源文件定義。推薦后者,即用xml文件方式,可以減少代碼量。
- 一般而言,菜單資源文件放在res目錄的menu目錄下(沒有規定,但約定俗成),其xml文件的根元素通常是<menu…/>,不需要指定任何屬性。
- 資源文件內容:
- <item…/> 元素:定義菜單項
- <group…/>子元素:將多個<item…/>定義的菜單項包裝成一個菜單組。
- 在文件中定義了菜單資源后,必須重寫onCreateOptionsMenu(),onCreateContextMenu() 方法,在這些方法中調用MenuInflater對象的inflate方法(填充器填充)使用指定資源文件對應的菜單就可以了。
- 資源文件內容:
- 活動條( ActionBar )
- 安卓3.0出現,位于傳統的標題欄的位置,也就是顯示在屏幕的頂部。其上一般顯示App的圖標和Activity標題。除此之外,ActionBar的右邊海可以顯示活動項( Action Item )。
- 但是我們一般不使用這種,所以我在前面的博客都是NoActionBar。
- Toast信息提示
- 顯示提示信息,如今的APP中最常見的是“再點擊一次退出”。(開發是用于事件是否成功的測試)
- 代碼測試
- package com.zc.helloworld;import android.content.Context;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends AppCompatActivity {private Button btn;private Context context;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn = (Button) findViewById(R.id.btn);context = this;btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(context,"安卓",Toast.LENGTH_LONG).show();}});}}
- SearchView搜索框
- 可以讓用戶在文本框輸入文字,通過監聽器監控用戶輸入,當用戶輸入完成后提交搜索,也可通過監聽器執行實際的搜索功能實現。
- 代碼測試
- <?xml version="1.0" encoding="utf-8"?><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="com.zc.helloworld.MainActivity"android:orientation="vertical"><SearchViewandroid:layout_width="match_parent"android:layout_height="wrap_content"></SearchView></LinearLayout>
- Fragment碎片
- 3.0版本引入了Fragment功能,類似于Activity,可以像Activity一樣包含布局。 不妨認為Fragment是一種輕量級的Activity,引入Fragment后,一個屏幕下布局更有了定制性和擴展性。曾今麻煩的多標簽實現有了更方便的用法。
- 這是一個相當常用的控件,很多實現都是基于它。
- 例如QQ的底部菜單,美團外賣的底部菜單導航。
- 補充說明
- 具體代碼可以在我的Github找到,歡迎star或者fork
總結
以上是生活随笔為你收集整理的安卓入门系列-07常用UI控件(长文)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓入门系列-06常见布局之Constr
- 下一篇: 安卓入门系列-08四大组件之Activi