1. TextView(文本视图)

TextView 是 Android 中常用的控件之一,用于显示文本内容,可以设置文字的样式、字体、颜色、大小等属性。

属性

以下是 TextView 常用的属性:

属性名

含义

示例

android:id

控件 ID

android:id=“@+id/textView”

android:background

设置背景颜色或背景图片

android:background=“#F0F0F0”

android:text

显示的文本内容

android:text=“Hello, world!”

android:textSize

字体大小

android:textSize=“24sp”

android:textColor

字体颜色

android:textColor=“#FF0000”

android:hint

当 TextView 显示的内容为空时,用于提示用户输入的文本

android:hint=“请输入用户名”

android:padding

控件内边距

android:padding=“10dp”

android:lines

文本行数,当文本行数超过指定行数时,会自动添加滚动条

android:lines=“5”

android:maxLines

最大文本行数

android:maxLines=“5”

android:minLines

最小文本行数

android:minLines=“2”

android:singleLine

是否单行显示

android:singleLine=“true”

android:ellipsize

文本超出行数时的省略方式

android:ellipsize=“end”

android:inputType

输入类型

android:inputType=“textPassword”

android:maxLength

最大输入长度

android:maxLength=“12”

常用方法

以下是常用的 Java 方法:

public class TextView extends View {

    // 设置显示的文本内容
    public void setText(CharSequence text);
    
    // 设置字体大小
    public void setTextSize(float size);
    
    // 设置字体颜色
    public void setTextColor(int color);
    
    // 设置当 TextView 显示的内容为空时,用于提示用户输入的文本
    public void setHint(CharSequence hint);
    
    // 设置内边距
    public void setPadding(int left, int top, int right, int bottom);
    
    // 设置文本行数
    public void setLines(int lines);
    
    // 设置最大文本行数
    public void setMaxLines(int maxLines);
    
    // 设置最小文本行数
    public void setMinLines(int minLines);
    
    // 设置是否单行显示
    public void setSingleLine(boolean singleLine);
    
    // 设置文本超出行数时的省略方式
    public void setEllipsize(TextUtils.TruncateAt where);
    
    // 设置输入类型
    public void setInputType(int type);
    
    // 设置最大输入长度
    public void setMaxLength(int maxLength);
    
    // 设置软键盘操作选项
    public void setImeOptions(int imeOptions);
    
    // 设置是否可用
    public void setEnabled(boolean enabled);
    
    // 设置是否可点击
    public void setClickable(boolean clickable);
    
    // 设置是否可聚焦
    public void setFocusable(boolean focusable);
    
    // 当控件处于触摸模式时,设置是否可聚焦
    public void setFocusableInTouchMode(boolean focusableInTouchMode);
    
    // 设置需要自动链接的类型,如电话号码、网址、电子邮件等
    public void setAutoLinkMask(int mask);
    
    // 图文混排时,设置文字与图片之间的间隔
    public void setCompoundDrawablePadding(int padding);
    
    // 在文本四周添加图标
    public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom);
}

2. EditText(编辑框)

EditText 继承自 TextView,是 Android 中常用的控件之一,用于输入文本内容,可以设置文字的样式、字体、颜色、大小等属性。

属性

以下 EditText 常用的属性:

属性名

含义

示例

android:id

控件 ID

android:id=“@+id/editText”

android:background

设置背景颜色或背景图片

android:background=“#F0F0F0”

android:text

显示的文本内容,若设置了 hint 属性,则显示的是 hint 内容,否则显示的是 text 内容

android:text=“请输入用户名”

android:textSize

字体大小

android:textSize=“18sp”

android:textColor

字体颜色

android:textColor=“#000000”

android:hint

当 EditText 显示的内容为空时,用于提示用户输入的文本

android:hint=“请输入用户名”

android:padding

控件内边距

android:padding=“10dp”

android:lines

文本行数,当文本行数超过指定行数时,会自动添加滚动条

android:lines=“3”

android:maxLines

最大文本行数

android:maxLines=“5”

android:minLines

最小文本行数

android:minLines=“2”

android:singleLine

是否单行显示

android:singleLine=“true”

android:ellipsize

文本超出行数时的省略方式

android:ellipsize=“end”

android:inputType

输入类型,如数字、电话号码、密码等

android:inputType=“textPassword”

android:maxLength

最大输入长度

android:maxLength=“12”

常用的方法:

以下是常用的java方法

public class EditText extends TextView {

    // 获取 EditText 中的文本内容
    public Editable getText();
    
    // 设置 EditText 中的文本内容
    public void setText(CharSequence text);
    
    // 设置字体大小
    public void setTextSize(float size);
    
    // 设置字体颜色
    public void setTextColor(int color);
    
    // 设置当 EditText 显示的内容为空时,用于提示用户输入的文本
    public void setHint(CharSequence hint);
    
    // 设置内边距
    public void setPadding(int left, int top, int right, int bottom);
    
    // 设置文本行数
    public void setLines(int lines);
    
    // 设置最大文本行数
    public void setMaxLines(int maxLines);
    
    // 设置最小文本行数
    public void setMinLines(int minLines);
    
    // 设置是否单行显示
    public void setSingleLine(boolean singleLine);
    
    // 设置文本超出行数时的省略方式
    public void setEllipsize(TextUtils.TruncateAt where);
    
    // 设置输入类型
    public void setInputType(int type);
    
    // 设置最大输入长度
    public void setMaxLength(int maxLength);
    
    // 设置软键盘操作选项
    public void setImeOptions(int imeOptions);
    
    // 设置是否可用
    public void setEnabled(boolean enabled);
    
    // 判断是否可编辑
    public boolean onCheckIsTextEditor();
    
    // 设置是否可编辑
    public void setEditable(boolean editable);
    
    // 设置是否可点击
    public void setClickable(boolean clickable);
    
    // 设置是否可聚焦
    public void setFocusable(boolean focusable);
    
    // 当控件处于触摸模式时,设置是否可聚焦
    public void setFocusableInTouchMode(boolean focusableInTouchMode);
    
    // 设置需要自动链接的类型,如电话号码、网址、电子邮件等
    public void setAutoLinkMask(int mask);
    
    // 图文混排时,设置文字与图片之间的间隔
    public void setCompoundDrawablePadding(int padding);
    
    // 在文本四周添加图标
    public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom);
}

3. Button(按钮)

Button 是 Android 中常用的控件之一,用于触发事件,可以设置按钮的样式、字体、颜色、大小等属性。

属性

以下是 Button 常用的属性:

属性名

含义

示例

android:id

控件 ID

android:id=“@+id/button”

android:text

显示的文本内容

android:text=“登录”

android:drawableLeft

在文本左侧添加一个图标

android:drawableLeft=“@drawable/icon”

android:drawableRight

在文本右侧添加一个图标

android:drawableRight=“@drawable/icon”

android:padding

控件内边距

android:padding=“10dp”

android:textSize

字体大小

android:textSize=“18sp”

android:textColor

字体颜色

android:textColor=“#000000”

android:background

设置背景颜色或背景图片

android:background=“#F0F0F0”

android:enabled

是否可用

android:enabled=“false”

android:clickable

是否可点击

android:clickable=“true”

常用方法

以下是常用的 Java 方法:

public class Button extends TextView {
    
    // 获取 Button 中的文本内容
    public CharSequence getText();
    
    // 设置 Button 的文本内容
    public void setText(CharSequence text);
    
    // 设置背景颜色或背景图片
    public void setBackground(Drawable background);
    
    // 使 Button 可用/不可用
    public void setEnabled(boolean enabled);
    
    // 设置按钮内边距
    public void setPadding(int left, int top, int right, int bottom);
    
    // 设置字体大小
    public void setTextSize(float size);
    
    // 设置字体颜色
    public void setTextColor(int color);
    
    // 在文本左侧添加一个图标
    public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom);
    
    // 是否可聚焦
    public void setFocusable(boolean focusable);
    
    // 是否可点击
    public void setClickable(boolean clickable);
    
    // 设置可视状态改变监听器
    public void setOnVisibilityChangedListener(OnVisibilityChangedListener listener);
    
    // 取消可视状态改变监听器
    public void unsetOnVisibilityChangedListener();
}

4. ImageView(图像视图)

ImageView 是 Android 中常用的控件之一,用于显示图片,可以设置图片的源、大小、缩放类型等属性。

属性

以下是 ImageView 常用的属性:

属性名

含义

示例

android:id

控件 ID

android:id=“@+id/imageView”

android:src

图片的源文件路径

android:src=“@drawable/image”

android:scaleType

缩放类型

android:scaleType=“centerCrop”

android:adjustViewBounds

调整 View 边界以适应其内容

android:adjustViewBounds=“true”

android:maxWidth

最大宽度

android:maxWidth=“200dp”

android:maxHeight

最大高度

android:maxHeight=“200dp”

android:layout_width

布局宽度

android:layout_width=“match_parent”

android:layout_height

布局高度

android:layout_height=“wrap_content”

常用方法

以下是常用的 Java 方法:

public class ImageView extends View {
    
    // 获取 ImageView 中显示的图片
    public Drawable getDrawable();
    
    // 设置显示的图片
    public void setImageDrawable(Drawable drawable);
    
    // 设置显示的位图
    public void setImageBitmap(Bitmap bm);
    
    // 获取 ImageView 的缩放类型
    public ScaleType getScaleType();
    
    // 设置 ImageView 的缩放类型
    public void setScaleType(ScaleType scaleType);
    
    // 获取 ImageView 的最大宽度
    public int getMaxWidth();
    
    // 设置 ImageView 的最大宽度
    public void setMaxWidth(int maxWidth);
    
    // 获取 ImageView 的最大高度
    public int getMaxHeight();
    
    // 设置 ImageView 的最大高度
    public void setMaxHeight(int maxHeight);
    
    // 是否调整 View 边界以适应其内容
    public boolean getAdjustViewBounds();
    
    // 设置是否调整 View 边界以适应其内容
    public void setAdjustViewBounds(boolean adjustViewBounds);
}

5. CheckBox(复选框)

CheckBox 是 Android 中常用的控件之一,用于表示二选一的选择状态。

属性

以下是 CheckBox 常用的属性:

属性名

含义

示例

android:id

控件 ID

android:id=“@+id/checkBox”

android:text

文本内容

android:text=“选项 A”

android:checked

是否选中

android:checked=“true”

android:button

用于选中状态显示的按钮图标

android:button=“@drawable/checked_button”

常用方法

以下是常用的 Java 方法:

public class CheckBox extends CompoundButton {
    
    // 判断 CheckBox 是否选中
    public boolean isChecked();
     
    // 设置 CheckBox 是否选中
    public void setChecked(boolean checked); 
    
    // 切换 CheckBox 状态
    public void toggle();  
}

6. RadioButton(单选按钮)

RadioButton 是 Android 中常用的控件之一,用于表示多选一的选择状态。

属性

以下是 RadioButton 常用的属性:

属性名

含义

示例

android:id

控件 ID

android:id=“@+id/radioButton”

android:text

文本内容

android:text=“选项 A”

android:checked

是否选中

android:checked=“true”

android:button

用于选中状态显示的按钮图标

android:button=“@drawable/checked_button”

常用方法

以下是常用的 Java 方法:

public class RadioButton extends CompoundButton {
    
    // 判断 RadioButton 是否选中
    public boolean isChecked();
     
    // 设置 RadioButton 是否选中
    public void setChecked(boolean checked); 
    
    // 切换 RadioButton 状态
    public void toggle();  
}

7. Spinner(下拉框)

Spinner 是 Android 中常用的控件之一,用于实现下拉列表的功能。

属性

以下是 Spinner 常用的属性:

属性名

含义

示例

android:id

控件 ID

android:id=“@+id/spinner”

android:entries

列表项文本内容,可以是静态或动态数组

android:entries=“@array/my_array”

android:prompt

显示在 Spinner 上方的提示性文本

android:prompt=“请选择一个选项”

android:background

在 Spinner 没有展开时显示的背景

android:background=“@drawable/spinner_bg”

常用方法

以下是常用的 Java 方法:

public class Spinner extends AbsSpinner implements AdapterView {
    
    // 获取 Spinner 中当前选择的项
    public Object getSelectedItem();
     
    // 设置 Spinner 的数据源
    public void setAdapter(SpinnerAdapter adapter); 
    
    // 添加子项
    public void addView(View child, ViewGroup.LayoutParams params);
}

8. ListView(列表视图)

ListView 是 Android 中常用的控件之一,用于实现列表视图。

属性

以下是 ListView 常用的属性:

属性名

含义

示例

android:id

控件 ID

android:id=“@+id/listView”

android:divider

列表项之间的分割线

android:divider=“@color/divider”

android:dividerHeight

分割线高度

android:dividerHeight=“2dp”

android:listSelector

点击时的背景效果

android:listSelector=“@drawable/item_bg”

常用方法

以下是常用的 Java 方法:

public class ListView extends AbsListView {
    
    // 获取列表项数目
    public int getCount();
     
    // 获取指定位置的列表项
    public Object getItem(int position); 
    
    // 获取指定位置的列表项 ID
    public long getItemId(int position);  
    
    // 设置数据源和适配器
    public void setAdapter(ListAdapter adapter);  
    
    // 设置选项单击事件监听器
    public void setOnItemClickListener(AdapterView.OnItemClickListener listener); 
    
    // 添加列表项
    public void addView(View child, ViewGroup.LayoutParams params);
}

9. ProgressBar(进度条)

ProgressBar 是 Android 中常用的控件之一,用于显示任务的进度。

属性

以下是 ProgressBar 常用的属性:

属性名

含义

示例

android:id

控件 ID

android:id=“@+id/progressBar”

android:indeterminate

是否为不确定模式,即转动圆圈

android:indeterminate=“true”

android:max

进度条的最大值

android:max=“100”

android:progress

当前进度

android:progress=“50”

android:progressDrawable

进度条的背景(未填充部分)

android:progressDrawable=“@drawable/progress_bg”

android:secondaryProgress

第二进度(一般用于双向进度条)

android:secondaryProgress=“25”

常用方法

以下是常用的 Java 方法:

public class ProgressBar extends View {
    
    // 获取进度条的最大值
    public int getMax();
     
    // 设置进度条的最大值
    public void setMax(int max); 
    
    // 获取进度条当前的进度
    public int getProgress();  
    
    // 设置进度条当前的进度
    public void setProgress(int progress);  
    
    // 判断进度条是否为不确定模式
    public boolean isIndeterminate(); 
    
    // 设置进度条是否为不确定模式
    public void setIndeterminate(boolean indeterminate);
}

10. Switch(开关按钮)

Switch 是 Android 中常用的控件之一,用于实现开关按钮。

属性

以下是 Switch 常用的属性:

属性名

含义

示例

android:id

控件 ID

android:id=“@+id/switchButton”

android:checked

开关状态(true 表示开启,false 表示关闭)

android:checked=“true”

android:textOff

关闭状态时的文本

android:textOff=“关”

android:textOn

开启状态时的文本

android:textOn=“开”

常用方法

以下是常用的 Java 方法:

public class Switch extends CompoundButton {
    
    // 获取开关状态
    public boolean isChecked();
     
    // 设置开关状态
    public void setChecked(boolean checked); 
    
    // 切换开关状态(即改变当前的状态)
    public void toggle();  
}