『安卓』安卓开发基础--基本控件
1. Textview
顯示文字,相當于Panel。一般用來文本展示,繼承自android.view.View,在android.widget包中。
<TextView//控件id android:id = "@+id/xxx" @+id/xxx表示新增控件命名為xxx //我們可以在Java代碼中通過findViewById()的方法獲取到該對象,然后進行相關屬性的設置,又或者使用RelativeLayout時,參考組件用的也是id!//寬度與高度 android:layout_width="wrap_content" //wrap_content或者match_parent android:layout_height="wrap_content" //wrap_content或者match_parent //wrap_content 控件顯示的內容多大,控件就多大 //match_parent(fill_parent) 填滿該控件所在的父容器 //當然也可以設置成特定的大小,//文本文字 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 android:background="" //控件的背景顏色,可以理解為填充整個控件的顏色,可以是圖片哦!//設置外邊距 android:layout_margin="10dp" 同時設置四個方向的外邊距 //同時可以單獨設置某個方向的外邊距。//設置內邊距 android:padding="30dp" 同時設置四個方向的內邊距 //同時可以單獨設置某個方向的內邊距。//設置重力方向 android:gravity="bottom|right" //可以設置四個方向//設置方向 android:orientation="horizontal" //可選值:horizontal 橫向 / vertical 縱向//相對布局 RelativeLayout:內部的控件以某個其他的控件為參考系,在參考系的某個方位。 //作為參考系的控件必須有一個名字(id) android:id="@+id/text1"//在名字叫text1的控件的右邊 android:layout_toRightOf="@id/text1" //在名字叫text1的控件的下邊 android:layout_below="@id/text1" //在名字叫text1的控件的左邊 android:layout_toLeftOf="@id/text1" //在名字叫text1的控件的上邊 android:layout_above="@id/text1" //比例劃分前提是線性布局,內部元素可以按照比例劃分 //需要設置權重: android:layout_weight="2" //縱向布局只能分內部控件的高度,橫向布局只能分內部控件的寬度。 //建議給要分比例的寬度或者高度寫成0dp。2. EditText
輸入框,可編輯,可設置軟鍵盤方式。繼承自android.widget.TextView,在android.widget包中。//控件id android:id = "@+id/xxx" @+id/xxx表示新增控件命名為xxx//寬度與高度 android:layout_width="wrap_content" //wrap_content或者match_parent android:layout_height="wrap_content" //wrap_content或者match_parent//文本文字 android:text="@string/hello_world" //兩種方式,直接具體文本或者引用values下面的string.xml里面的元素//文本提示內容 android:hint="hello_world" //android:text和android:hint區別是后者只是提示作用,真正需要輸入的時候提示的內容會消失//字體大小 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//輸入內容設置為password類型 android:password="true" //輸入的內容會變成······//輸入內容設置為phoneNumber類型 android:phoneNumber="true" //只能輸入數字//設定光標為顯示/隱藏 android:cursorVisible = "false" //true或者false,默認為true顯示//設置外邊距 android:layout_margin="10dp" 同時設置四個方向的外邊距 //同時可以單獨設置某個方向的外邊距。//設置內邊距 android:padding="30dp" 同時設置四個方向的內邊距 //同時可以單獨設置某個方向的內邊距。//設置重力方向 android:gravity="bottom|right" //可以設置四個方向//設置方向 android:orientation="horizontal" //可選值:horizontal 橫向 / vertical 縱向//相對布局 RelativeLayout:內部的控件以某個其他的控件為參考系,在參考系的某個方位。 //作為參考系的控件必須有一個名字(id) android:id="@+id/text1"//在名字叫text1的控件的右邊 android:layout_toRightOf="@id/text1" //在名字叫text1的控件的下邊 android:layout_below="@id/text1" //在名字叫text1的控件的左邊 android:layout_toLeftOf="@id/text1" //在名字叫text1的控件的上邊 android:layout_above="@id/text1"在Activity中的簡單用法
public class MainActivity extends Activity {//聲明一個EditTextprivate EditText edittext;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//給當前的活動加載一個布局setContentView(R.layout.activity_main);//初始化edittextedittext=(EditText) findViewById(R.id.edit_text);}... ...//在方法中調用給edittext賦值edittext.setText("success"); ... ... }3. Button
<Button//控件id android:id = "@+id/xxx" @+id/xxx表示新增控件命名為xxx//寬度與高度 android:layout_width="wrap_content" //wrap_content或者match_parent android:layout_height="wrap_content" //wrap_content或者match_parent//按鈕上顯示的文字 android:text="theButton" //兩種方式,直接具體文本或者引用values下面的string.xml里面的元素@string/button//按鈕字體大小 android:textSize="24sp" //以sp為單位//字體顏色 android:textColor="#0000FF" //RGB顏色//字體格式 android:textStyle="normal" //normal,bold,italic分別為正常,加粗以及斜體,默認為normal//是否只在一行內顯示全部內容 android:singleLine="true" //true或者false,默認為false//設置外邊距 android:layout_margin="10dp" 同時設置四個方向的外邊距 //同時可以單獨設置某個方向的外邊距。//設置內邊距 android:padding="30dp" 同時設置四個方向的內邊距 //同時可以單獨設置某個方向的內邊距。//設置重力方向 android:gravity="bottom|right" //可以設置四個方向//相對布局 RelativeLayout:內部的控件以某個其他的控件為參考系,在參考系的某個方位。 //作為參考系的控件必須有一個名字(id) android:id="@+id/text1"//在名字叫text1的控件的右邊 android:layout_toRightOf="@id/text1" //在名字叫text1的控件的下邊 android:layout_below="@id/text1" //在名字叫text1的控件的左邊 android:layout_toLeftOf="@id/text1" //在名字叫text1的控件的上邊 android:layout_above="@id/text1"我們需要在Activity中為Button的點擊事件注冊一個監聽器,以下介紹兩種方式來實現按鈕監聽事件,更多方法可以參考下Android的按鈕單擊事件及監聽器的實現方式,跟JAVA JFrame監聽類似。
1.通過匿名內部類作為事件監聽器類,這種方法適用于事件監聽器只是臨時使用一次,因為大部分時候,事件處理器都沒有什么利用價值(可利用代碼通常都被抽象成了業務邏輯方法),這是一種使用最廣泛的方法:
public class MainActivity extends Activity {private EditText edittext;private Button button;@Overrideprotected 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() {@Overridepublic void onClick(View v) {//點擊Button會改變edittext的文字為"點擊了Button"edittext.setText("點擊了Button");}}); } }2.使用實現接口的方式來進行注冊,讓Activity類實現了OnClickListener事件監聽接口,從而可以在該Activity類中直接定義事件處理器方法:onClick(view v),當為某個組件添加該事件監聽器對象時,直接使用this作為事件監聽器對象即可:
public class MainActivity extends Activity implements OnClickListener {private EditText edittext;private Button button;private Button button2;@Overrideprotected 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);button2 = (Button) findViewById(R.id.button2);button.setOnClickListener(this);button2.setOnClickListener(this);}@Override//用switch區分是哪個idpublic void onClick(View v) {switch (v.getId()){case R.id.button:edittext.setText("點擊了Button");break;case R.id.button2:edittext.setText("點擊了Button2");break;}} }4.ImageButton
ImageButton繼承自ImageView類,與Button之間的最大區別在于ImageButton中沒有text屬性。ImageButton控件中設置按鈕中顯示的圖片可以通過android:src屬性來設置。也可以通過setImageResource(int)來設置。
<ImageButton//控件id android:id = "@+id/xxx" @+id/xxx表示新增控件命名為xxx//寬度與高度 android:layout_width="wrap_content" //wrap_content或者match_parent android:layout_height="wrap_content" //wrap_content或者match_parent//此外,可以具體設置高度和寬度顯示的像素,不過這樣設置如果圖片尺寸大于設置的顯示的尺寸,則圖片是顯示不全的,這是可以配合android:scaleType屬性。 android:layout_width="200dp" android:layout_height="200dp" //把原圖按照指定的大小在View中顯示,拉伸顯示圖片,不保持原比例,填滿ImageButton. android:scaleType="fitXY"//其他的關于android:scaleType的參數解釋,也可以參考下面的直觀圖 //android:scaleType="center" 在視圖中心顯示圖片,并且不縮放圖片 //android:scaleType="centercrop" 按比例縮放圖片,使得圖片長 (寬)的大于等于視圖的相應維度 //android:scaleType="centerinside" 按比例縮放圖片,使得圖片長 (寬)的小于等于視圖的相應維度 //android:scaleType="fitcenter" 按比例縮放圖片到視圖的最小邊,居中顯示 //android:scaleType="fitend" 按比例縮放圖片到視圖的最小邊,顯示在視圖的下部分位置 //android:scaleType="fitstart" 把圖片按比例擴大/縮小到視圖的最小邊,顯示在視圖的上部分位置 //android:scaleType="matrix" 用矩陣來繪制//圖片來源,需要將圖片復制放到res/drawable文件夾里面,引用的時候不需要寫圖片的后綴 android:src ="@drawable/beautiful"> //設置外邊距 android:layout_margin="10dp" 同時設置四個方向的外邊距 //同時可以單獨設置某個方向的外邊距。//設置內邊距 android:padding="30dp" 同時設置四個方向的內邊距 //同時可以單獨設置某個方向的內邊距。//設置重力方向 android:gravity="bottom|right" //可以設置四個方向//相對布局 RelativeLayout:內部的控件以某個其他的控件為參考系,在參考系的某個方位。 //作為參考系的控件必須有一個名字(id) android:id="@+id/text1"//在名字叫text1的控件的右邊 android:layout_toRightOf="@id/text1" //在名字叫text1的控件的下邊 android:layout_below="@id/text1" //在名字叫text1的控件的左邊 android:layout_toLeftOf="@id/text1" //在名字叫text1的控件的上邊 android:layout_above="@id/text1"5.RadioGroup和RadioButton
RadioGroup 多選
RadioButton 單選選項
6.CheckBox
復選框
<CheckBox android:id="@+id/RB1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/radiobutton1" //選項文字android:checked="ture" //是否默認選中7.ImageView
ImageView控件負責顯示圖片,其圖片的來源可以是在資源文件中的id,也可以是Drawable對象或者位圖對象。還可以是Content Provider的URI。
<ImageView //控件id android:id = "@+id/xxx" @+id/xxx表示新增控件命名為xxx//寬度與高度 android:layout_width="wrap_content" //wrap_content或者match_parent android:layout_height="wrap_content" //wrap_content或者match_parent//此外,可以具體設置高度和寬度顯示的像素,不過這樣設置如果圖片尺寸大于設置的顯示的尺寸,則圖片是顯示不全的,這是可以配合android:scaleType屬性。 android:layout_width="200dp" android:layout_height="200dp" //把原圖按照指定的大小在View中顯示,拉伸顯示圖片,不保持原比例,填滿ImageButton. android:scaleType="fitXY"//其他的關于android:scaleType的參數解釋,也可以參考下面的直觀圖 //android:scaleType="center" 在視圖中心顯示圖片,并且不縮放圖片 //android:scaleType="centercrop" 按比例縮放圖片,使得圖片長 (寬)的大于等于視圖的相應維度 //android:scaleType="centerinside" 按比例縮放圖片,使得圖片長 (寬)的小于等于視圖的相應維度 //android:scaleType="fitcenter" 按比例縮放圖片到視圖的最小邊,居中顯示 //android:scaleType="fitend" 按比例縮放圖片到視圖的最小邊,顯示在視圖的下部分位置 //android:scaleType="fitstart" 把圖片按比例擴大/縮小到視圖的最小邊,顯示在視圖的上部分位置 //android:scaleType="matrix" 用矩陣來繪制//圖片來源,需要將圖片復制放到res/drawable文件夾里面,引用的時候不需要寫圖片的后綴 android:src ="@drawable/beautiful"> //設置外邊距 android:layout_margin="10dp" 同時設置四個方向的外邊距 //同時可以單獨設置某個方向的外邊距。//設置內邊距 android:padding="30dp" 同時設置四個方向的內邊距 //同時可以單獨設置某個方向的內邊距。//設置重力方向 android:gravity="bottom|right" //可以設置四個方向//設置方向 android:orientation="horizontal" //可選值:horizontal 橫向 / vertical 縱向//相對布局 RelativeLayout:內部的控件以某個其他的控件為參考系,在參考系的某個方位。 //作為參考系的控件必須有一個名字(id) android:id="@+id/text1"//在名字叫text1的控件的右邊 android:layout_toRightOf="@id/text1" //在名字叫text1的控件的下邊 android:layout_below="@id/text1" //在名字叫text1的控件的左邊 android:layout_toLeftOf="@id/text1" //在名字叫text1的控件的上邊 android:layout_above="@id/text1"7.ProgressBar
ProgressBar 用于在界面上顯示一個進度條,體現程序運行時正在加載數據。
在布局文件中使用:
<ProgressBar android:id="@+id/pb"android:layout_width="match_parent"android:layout_height="wrap_content"//默認是圓形進度條,可以知道樣式設置為水平進度條style="?android:attr/progressBarStyleHorizontal"/>//指定成水平進度條后,我們還可以通過 android:max屬性給進度條設置一個最大值,然后在代碼中動態地更改進度條的進度android:max="100"借助控件可見性,實現數據加載完成時消失。
借助 setVisibility()方法,可以傳入 View.VISIBLE、View.INVISIBLE 和 View.GONE 三種值。
下面實現點擊一下按鈕讓進度條消失,再點擊一下按鈕讓進度條出現的這種效果,這里只給出按鈕監聽的代碼:
button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//通過 getVisibility()方法來判斷 ProgressBar 是否可見if (progressBar.getVisibility() == View.GONE) {progressBar.setVisibility(View.VISIBLE);} else {progressBar.setVisibility(View.GONE);}}});參考博客:Android常用控件介紹及使用
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的『安卓』安卓开发基础--基本控件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 虽迟但到,微星 Radeon RX 79
- 下一篇: iQOO Neo7 SE 手机 16GB