Android入門(3)| 基本控件和布局

本節(jié)目錄

控件的使用方法

AS中為我們開發(fā)者提供了可視化的圖形編輯器,通過它我們可以手動(dòng)的拖拽和選擇所需要的控件以及要擺放的位置。但是我們更經(jīng)常使用的則是XML來自己編寫界面。

在使用控件之前,我們先來了解一下layout,找到src—res—layout,打開它的子目錄就會(huì)出現(xiàn)布局編輯器:


布局編輯器

我們選擇左下角的text,就會(huì)跳轉(zhuǎn)到代碼編輯區(qū),我們就在這里使用XML來自己手寫界面。而在它的右邊會(huì)有一個(gè)手機(jī)的界面,每當(dāng)我們完成界面的編寫,它都會(huì)第一時(shí)間的顯示出來供我們參考修改。


代碼編輯區(qū)

基本控件的介紹

AS提供了大量的UI控件,下面就來介紹一些常用的控件以及它們的使用方法:

1.TextView 文本框

TextView主要是用于在界面上顯示一段文本信息。在代碼編輯區(qū)中寫下如下代碼:

<TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 

        android:text="TextView"
        android:gravity="center"
        android:textSize="20sp"
        android:textColor="#00ff00"/>

下面來解釋代碼的具體含義,首先是android:id,它是給當(dāng)前控件一個(gè)唯一的標(biāo)識(shí)符,在活動(dòng)中調(diào)用控件時(shí)就會(huì)使用該標(biāo)識(shí)符;android:layout_widthandroid:layout_height是控制控件的寬度和高度,一般就只有match_parentwrap_content,match_parent是指讓控件的大小與父布局的大小一致(即手機(jī)的大小),而wrap_content是指控件的大小剛好能夠包住里面的內(nèi)容。前面三個(gè)是所有控件都擁有的屬性,后面的控件中我們就不多介紹這三個(gè)了。

接著是介紹TextView中所獨(dú)有(有些控件可能也會(huì)使用,但不是所有的控件都能使用)的屬性了:
android:text:是為TextView來指定所顯示的內(nèi)容的,我們可以自己編輯。
android:gravity:來指定文字的對(duì)齊方式,可選的主要有:topbottomleferightcenter等。
android:textSize:用來指定文字的大小,單位是sp。
android:textColor:用來指定文字的顏色,一般填入的都是顏色的代碼。

2.Button 按鈕

按鈕就不多說了,直接開始介紹。在編輯區(qū)中輸入以下代碼:

 <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:text="Button"/>

android:text也可以使用在Button中。

3.EditText 可輸入文本框

EditText是允許用戶在輸入框里面輸入內(nèi)容的組件,同樣輸入代碼:

<EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        
        android:hint="請(qǐng)輸入密碼"
        android:maxLines="2"/>

android:hint:是EditText的獨(dú)有屬性,它是指在輸入時(shí)輸入框會(huì)出現(xiàn)一段比較淺的提示語來提醒你輸入什么,這一段提示語我們就可以使用android:hint來實(shí)現(xiàn)。
android:maxLines:指我們?cè)佥斎霑r(shí)可以顯示的行數(shù),在這里我們?cè)O(shè)定的顯示是2行。

4.imagineView 圖片

imagineView是我們用來存放圖片的控件,在目錄res中有drawable子目錄來專門存放我們所需要的圖片,我們將自己的圖片命名為img_1.jpg后拖入到該目錄當(dāng)中,然后輸入代碼:

<ImageView
        android:id="@+id/imagine_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@drawable/img_1"/>

android:src:是指調(diào)用drawable中的圖片來顯示到手機(jī)界面上,其中/后面需要寫所需要調(diào)用圖片的名稱。

5.ProgressBar 進(jìn)度條

ProgressBar就是類似于進(jìn)度條的控件,它的使用方法是:

 <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        
        android:visibility="gone"/>

android:visibility:是一個(gè)公共屬性,它的具體功能是控制該控件是否是可見的,一般是有三個(gè)選項(xiàng):visible、invisiblegonevisible指的是可見,如果不特意寫出來所有的控件默認(rèn)都是visibleinvisible則是不可見,但是這里的“不可見”可以看做是透明,即雖然看不到,但確是真實(shí)存在的,最后是gone,它就是指徹徹底底的看不見(并不是透明了)。

基本布局的介紹

布局簡單來說就是控制控件的擺放方式“容器”,布局可以指定每個(gè)控件的擺放方式,包括大小,位置,所占的百分比等。Android中有4種基本的布局,下面就來一一了解它們吧。

1.LinearLayout 線性布局

線性布局是我們最常使用的一種布局方式,在上上面介紹控件的時(shí)候我們就是使用的線性布局。找到res—layout,然后點(diǎn)擊右鍵,找到new—layout resource file點(diǎn)擊之后就會(huì)出現(xiàn)創(chuàng)建布局的面板,我們將布局命名為linear_layout(注意必須是小寫)將布局方式選擇為LinearLayout:


創(chuàng)建布局

創(chuàng)建完成之后我們修改代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:text="button1"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="button2"/>
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="button3"/>

</LinearLayout>

完成之后我們會(huì)發(fā)現(xiàn)右邊的手機(jī)視圖出現(xiàn)了三個(gè)水平排列的按鈕,這就是LinearLayout的功勞了。分析代碼,我們可以看到在開頭中有android:orientation="vertical"這一行,而這就是LinearLayout的專有屬性了,android:orientation可以控制控件的排列方式,一般是有2種方式可供選擇:vertical(垂直排列)horizontal(水平排列)。但是要注意如果選擇的排列方式是vertical(垂直排列),則控件中的android:layout_height就不能選擇match_parent了,因?yàn)榇藭r(shí)控件的擺放方式是受布局的影響的,同理horizontal(水平排列)時(shí)就不能在android:layout_width上使用match_parent。

接著再來介紹有關(guān)線性布局的其他重要屬性:
layout_gravity:可以看到它和之前的gravity有一點(diǎn)像,它們之間的區(qū)別是layout_gravity是指控件之間的相對(duì)布局,而gravity則是文字在控件中的相對(duì)位置。layout_gravity有很多個(gè)選項(xiàng),一般我們會(huì)使用的有:topcenterbottom。

接著我們修改代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="input"/>
    
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button"/>

</LinearLayout>

可以看到我們使用了android:layout_weight這個(gè)屬性,這個(gè)屬性是指控件在寬度上的相對(duì)大小,在這里就是EditText和Button各占一半了(因?yàn)槎际窃O(shè)置的為1),但是要注意我們?cè)谶@里將控件的android:layout_width都設(shè)置成了0dp,因?yàn)榇藭r(shí)我們需要控件是由android:layout_weight來控制,而設(shè)置為0dp是一種標(biāo)準(zhǔn)的寫法。

2.RelativeLayout 相對(duì)布局

相對(duì)布局可以讓我們對(duì)控件有更加隨意控制。相對(duì)布局主要有2種方式:相對(duì)父布局和相對(duì)控件布局。我們?cè)賱?chuàng)建一個(gè)布局,然后選擇布局模式為RelativeLayout ,然后修改代碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button1"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button2"/>
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button3"/>
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:text="Button4"/>
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="Button5"/>

</RelativeLayout>

其實(shí)這樣大致看下來就會(huì)很簡單,就是在控件中加上android:layout_alignParentRight、android:layout_alignParentLeft等等相對(duì)于父布局的位置,然后設(shè)定為true就可以,如果不設(shè)定一般默認(rèn)為false。

除此以外,我們還可以相對(duì)于控件來進(jìn)行布局,修改代碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button3"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_toLeftOf="@+id/button3"
        android:text="Button1"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_toRightOf="@+id/button3" 
        android:text="Button2"/>
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button3"
        android:layout_toLeftOf="@+id/button3"
        android:text="Button4"/>
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button3"
        android:layout_toRightOf="@+id/button3" 
        android:text="Button5"/>

</RelativeLayout>

這個(gè)也是很好理解的,就是Button1\2\4\5放在Button3的四周,但是需要注意的是在設(shè)置屬性時(shí)需要將Button3的id傳進(jìn)去,因此我們需要將Button3先設(shè)置在最前面,放置后面的控件找不到Button3的id。

3.FrameLayout 幀布局

幀布局是最簡單的一種布局了,它就是簡單的將控件默認(rèn)放在整個(gè)布局的左上角。我們一起來看一看吧:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"/>
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>

</FrameLayout>

通過左邊的視圖可以看到按鈕和圖片都出現(xiàn)在了左上角,但是這樣會(huì)使得圖片遮蓋住后面的按鈕,所以這種布局我們不常使用。

4.百分比布局

百分比布局是Android引入的一種權(quán)限全新的布局方式,在這種布局中,我們可以不使用wrap_cntentmatch_partent等方式來控制控件的大小,而是直接允許設(shè)置控件在布局中所占的百分比。因?yàn)長inearLayout已經(jīng)允許設(shè)定控件的百分比,所以百分比布局主要是為Relati veLayout和FramLayout提供擴(kuò)展,分別是PercentRelativeLayout和PercentFramLayout。

要使用百分比布局,我們首先需要在build.gradle中添加百分比布局的依賴。打開app—build.gradle,然后在dependencies修改成如下代碼:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    compile 'com.android.support:percent:26.1.0'   
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

之后點(diǎn)擊上方的sync now后更新一下,我們就可以使用百分比布局了。創(chuàng)建一個(gè)新的布局,然后修改代碼:

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:text="Button1"
        android:layout_gravity="left|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"/>
    <Button
        android:id="@+id/button2"
        android:text="Button2"
        android:layout_gravity="right|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"/>
    <Button
        android:id="@+id/button3"
        android:text="Button3"
        android:layout_gravity="left|bottom"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"/>
    <Button
        android:id="@+id/button4"
        android:text="Button4"
        android:layout_gravity="right|bottom"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"/>

</android.support.percent.PercentFrameLayout>

其實(shí)代碼的整體部分是比較好理解的,這里就不多說了。需要注意的是在開頭需要設(shè)定一個(gè)命名空間app,然后在使用的時(shí)候需要在前面加上命名空間,之后就可以正常的使用了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,355評(píng)論 0 17
  • 歡迎Follow我的GitHub, 關(guān)注我的CSDN. 其余參考Android目錄. 轉(zhuǎn)載請(qǐng)注明出處:http:/...
    passiontim閱讀 4,939評(píng)論 0 31
  • 內(nèi)容 抽屜菜單 ListView WebView SwitchButton 按鈕 點(diǎn)贊按鈕 進(jìn)度條 TabLayo...
    小狼W閱讀 1,671評(píng)論 0 10
  • Android功能強(qiáng)大,界面華麗,但是眾多的布局屬性就害苦了開發(fā)者,下面這篇文章結(jié)合了網(wǎng)上不少資料.第一類:屬性值...
    HangChen閱讀 5,188評(píng)論 0 24
  • 放不下筆 恰如同放不下你 放不下的夢(mèng)想 還不是得去堅(jiān)持 臉上寫滿了戲 內(nèi)心卻是白紙 寫 一首詩 三言兩語 七律絕句...
    字不悔閱讀 265評(píng)論 0 2

友情鏈接更多精彩內(nèi)容