轉自:http://www.tuicool.com/articles/nE3mma
從零開始學android<TextView的使用.四.>
基本的圖形組件View
No.
|
組件名稱
|
描述
|
1
|
TextView
|
表示一個文本的顯示組件 |
2
|
Button
|
表示一個普通的按鈕 |
3
|
EditText
|
表示一個可編輯的文本框組件 |
4
|
CheckBox
|
表示複選框組件
|
5
|
RadioGroup
|
表示單選鈕組件
|
6
|
Spinner
|
下拉列表框
|
7
|
DatePicker
|
日期選擇組件
|
8
|
TimePicker
|
時間選擇組件
|
9
|
ScrollView
|
滾動條
|
10
|
ProgressBar
|
進度處理條
|
11
|
SeekBar
|
拖動條組件
|
12
|
RatingBar
|
評分組件
|
13
|
ImageView
|
圖片顯示組件
|
14
|
ImageButton
|
圖片按鈕
|
15
|
AutoCompleteTextView |
自動完成文本組件 |
16
|
Dialog
|
對話框組件
|
17
|
Toast
|
信息提示框組件
|
18
|
Menu
|
菜單顯示組件
|
組件的屬性設置
No.
|
屬性名稱
|
方法名稱
|
描述
|
1
|
android:background |
public void setBackgroundResource (int resid) |
設置組件背景
|
2
|
android:clickable |
public void setClickable (boolean clickable) |
是否可以產生單擊事件 |
3
|
android:contentDescription |
public void setContentDescription (CharSequence contentDescription) |
定義視圖的內容描述 |
4
|
android:drawingCacheQuality |
public void setDrawingCacheQuality (int quality) |
設置繪圖時所需要的緩衝區大小 |
5
|
android:focusable |
public void setFocusable (boolean focusable) |
設置是否可以獲得焦點 |
6
|
android:focusableInTouchMode |
public void setFocusableInTouchMode (boolean focusableInTouchMode) |
在觸摸模式下配置是否可以獲得焦點 |
7
|
android:id
|
public void setId (int id) |
設置組件ID
|
8
|
android:longClickable |
public void setLongClickable (boolean longClickable) |
設置長按事件是否可用 |
9
|
android:minHeight |
定義視圖的最小高度 |
|
10
|
android:minWidth |
定義視圖的最小寬度 |
11
|
android:padding |
public void setPadding (int left, int top, int right, int bottom) |
填充所有的邊緣
|
12
|
android:paddingBottom |
public void setPadding (int left, int top, int right, int bottom) |
填充下邊緣
|
13
|
android:paddingLeft |
public void setPadding (int left, int top, int right, int bottom) |
填充左邊緣
|
14
|
android:paddingRight |
public void setPadding (int left, int top, int right, int bottom) |
填充右邊緣
|
15
|
android:paddingTop |
public void setPadding (int left, int top, int right, int bottom) |
填充上邊緣
|
16
|
android:scaleX
|
public void setScaleX (float scaleX) |
設置X軸縮放
|
17
|
android:scaleY
|
public void setScaleY (float scaleY) |
設置Y軸縮放
|
18
|
android:scrollbarSize |
設置滾動條大小
|
|
19
|
android:scrollbarStyle |
public void setScrollBarStyle (int style) |
設置滾動條樣式
|
20
|
android:visibility |
public void setVisibility (int visibility) |
設置是否顯示組件 |
21
|
android:layout_width |
定義組件顯示的寬度 |
|
22
|
android:layout_height |
定義組件顯示的長度 |
|
23
|
android:layout_gravity |
組件文字的對齊位置 |
|
24
|
android:layout_margin |
設置文字的邊距
|
|
25
|
android:layout_marginTop |
上邊距
|
|
26
|
android:layout_marginBottom |
下邊距
|
|
27
|
android:layout_marginLeft |
左邊距
|
|
28
|
android:layout_marginRight |
右邊距
|
|
29
|
android:background |
設置背景顏色
|
今天我麼來看一下TextView 和EditText組件的基本使用。
TextView組件常用的設置方法
No.
|
配置屬性名稱
|
對應方法
|
描述
|
1
|
android:text
|
public final void setText (CharSequence text) |
定義組件的顯示文字 |
2
|
android:maxLength |
public void setFilters (InputFilter[] filters) |
設置組件最大允許長度 |
3
|
android:textColor |
public void setTextColor (ColorStateList colors) |
設置組件的文本顏色 |
4
|
android:textSize |
public void setTextSize (float size) |
設置顯示的文字大小 |
5
|
android:textStyle |
設置文字顯示的樣式,粗體、斜體等 |
|
6
|
android:selectAllOnFocus |
public void setSelectAllOnFocus (boolean selectAllOnFocus) |
默認選中並獲得焦點 |
7
|
android:password |
public final void setTransformationMethod (TransformationMethod method) |
按密文方式顯示文本信息 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"//絕對佈局 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:textColor="#ff00ffff//設置字體顏色 android:layout_height="wrap_content"//包裹內容 android:layout_width="wrap_content"//包裹內容 android:text="www.csdn.net"//顯示內容 android:autoLink="all"//允許連接形式存在 android:background="#FF00FF00"//設置背景顏色 /> </RelativeLayout>
在Android中所有的組件可以設置大小,但是在設置大小的時候需要指定其單位,這些單位如下:
px ( pixels ):像素;
dip ( device independent pixels ):依賴於設備的像素;
sp ( scaled pixels —— best for text size ):帶比例的像素;
pt ( points ):點;
in ( inches ):英尺;
mm ( millimeters ):毫米。
在JAVA文件中進行改變
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView info=null;//聲明TextView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.info=(TextView)super.findViewById(R.id.info);//找到textview
info.setText("我在JAVA文件中已經改變了,tettview原來的值");//設置屬性
info.setTextSize(20);
}
}
好了,txteview組件先簡單介紹到這裏,隨後我們用到時候會具體介紹。
下節預報:Button組件的使用