【ALearning】第三章 Android基本常見控制項

來源:互聯網
上載者:User

標籤:android   布局   

       本章主要介紹基本的平常較多使用的控制項,包括TextView、EditView、ImageView、Button等。本章將介紹相關控制項基本屬性的使用,為以後章節的進階學習提供基礎。案例中引用的LinearLayout布局,可先不必深究,後續章節將會詳細介紹。

TextViewTextView控制項的基本屬性,android:layout_width 布局寬度android:layout_height 布局高度。這兩個屬性參數是必須的。
TextView 中android:layout_width與android:layout_height可供選擇的參數:
  • match_parent   :匹配父控制項大小(寬、高)【官方建議使用】
  • fill_parent    :填充父控制項大小(寬、高)等效於match_parent
  • wrap_content   :包裹內容
TextView擴充的屬性:
  • android:text指定控制項的文本
  • android:gravity指定控制項的基本位置
  • android:drawableLef指定text左邊輸出的drawable(片)
  • android:drawableRight指定text右邊輸出的drawable(片)
  • android:drawableTop指定text頂部輸出的drawable(片)
  • android:drawableBottom指定text底部輸出的drawable(片)
【布局檔案】activity_textview.xml。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="TextView" />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:drawableLeft="@drawable/tupiao"        android:gravity="center"        android:text="TextView_drawableLeft" />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:drawableRight="@drawable/tupiao"        android:gravity="center"        android:text="TextView_drawableRight" />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:drawableTop="@drawable/tupiao"        android:gravity="center"        android:text="TextView_drawableTop" />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:drawableBottom="@drawable/tupiao"        android:gravity="center"        android:text="TextView_drawableBottom" /></LinearLayout>

效果:


EditText

EditText控制項,主要提供使用者填寫(輸入)資訊的介面作用,而之前介紹的TextView主要作用是資訊的顯示。
EditText中android:layout_width與android:layout_height可供選擇的參數:
  • match_parent   :匹配父控制項大小(寬、高)【官方建議使用】
  • fill_parent    :填充父控制項大小(寬、高)等效於match_parent
  • wrap_content   :包裹內容
EditText擴充的屬性:
  • android:text 指定控制項的文本
  • android:gravity 指定控制項的基本位置
  • android:password 指定文本以小點“?”顯示
  • android:hint 指定Text為空白時顯示的文字提示資訊
  • android:drawableLef 指定text左邊輸出的drawable(片)
  • android:drawableRight 指定text右邊輸出的drawable(片)
  • android:drawableTop 指定text頂部輸出的drawable(片)
  • android:drawableBottom 指定text底部輸出的drawable(片)
【布局檔案】activity_edittext.xml。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="EditText" />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:hint="please input your message! " />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:drawableTop="@drawable/tupiao"        android:gravity="center"        android:hint="please input your message! " />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:drawableTop="@drawable/tupiao"        android:gravity="center"        android:text="EditText" />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:drawableLeft="@drawable/tupiao"        android:gravity="center"        android:password="true"        android:text="EditText" /></LinearLayout>
效果:


ImageView

ImageView控制項,主要提供圖片顯示的作用,但當對基本的控制項瞭解清楚之後,就會發現這些基本控制項很多都可以通用適用,例如按鈕的點擊事件監聽操作。
ImageView中android:layout_width與android:layout_height可供選擇的參數:
  • match_parent   :匹配父控制項大小(寬、高)【官方建議使用】
  • fill_parent    :填充父控制項大小(寬、高)等效於match_parent
  • wrap_content   :包裹內容
ImageView擴充的屬性:
  • android:src 指定前景drawable(片)
  • android:background 指定背景drawable(片)
註:background會根據ImageView組件給定的長寬進行展開,而src就存放的是原圖的大小,不會進行展開。src是圖片內容(前景),background是背景,可以同時使用。
【布局檔案】activity_imageview.xml。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ImageView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:src="@drawable/image" />    <ImageView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@android:color/background_dark"        android:src="@drawable/image" /></LinearLayout>

效果:


Button    ImageButton

Button控制項,從命名本身就可以很明了,提供按鈕的控制項,而ImageButton更顯而易見是提供圖片按鈕。
Button/ImageButton中android:layout_width與android:layout_height可供選擇的參數:
  • match_parent   :匹配父控制項大小(寬、高)【官方建議使用】
  • fill_parent    :填充父控制項大小(寬、高)等效於match_parent
  • wrap_content   :包裹內容
Button/ImageButton擴充的屬性:
  • android:src 指定前景drawable(片)
  • android:text     對於ImageButton控制項無效
  • android:background 指定前景drawable(片)
【布局檔案】activity_button.xml。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Button" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@drawable/button"        android:text="Button" />    <ImageButton        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@drawable/button_with_text" /></LinearLayout>

效果:

            Android常用的控制項有很多種,本章主要介紹這些較為常用的控制項,以方便後續章節的使用與為新知識點的引入做鋪墊。本章的學習就此結束,敬請期待下一章節。

參考資料1、http://www.uuton.com/post/3f493_1b87532、http://blog.163.com/allegro_tyc/blog/static/33743768201371301526104/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.