Android常用控件 #

一、TextView #

TextView用于显示文本内容。

1.1 基本用法 #

xml
<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World"
    android:textSize="16sp"
    android:textColor="#000000" />

1.2 常用属性 #

属性 说明
android:text 文本内容
android:textSize 文字大小(sp)
android:textColor 文字颜色
android:textStyle 文字样式:normal/bold/italic
android:textAlignment 文本对齐
android:gravity 内容对齐方式
android:maxLines 最大行数
android:ellipsize 省略位置:start/middle/end/marquee
android:lineSpacingExtra 行间距
android:letterSpacing 字符间距
android:shadowColor 阴影颜色
android:shadowDx 阴影X偏移
android:shadowDy 阴影Y偏移
android:shadowRadius 阴影半径

1.3 富文本 #

kotlin
val textView = findViewById<TextView>(R.id.textView)

// 使用SpannableString
val spannable = SpannableString("红色加粗斜体")
spannable.setSpan(ForegroundColorSpan(Color.RED), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
spannable.setSpan(StyleSpan(Typeface.BOLD), 2, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
spannable.setSpan(StyleSpan(Typeface.ITALIC), 4, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

textView.text = spannable

// 使用Html
textView.text = Html.fromHtml("<b>粗体</b> <i>斜体</i> <u>下划线</u>", Html.FROM_HTML_MODE_COMPACT)

1.4 点击链接 #

xml
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="访问 https://www.example.com"
    android:autoLink="web" />
kotlin
// 代码设置
textView.movementMethod = LinkMovementMethod.getInstance()
val spannable = SpannableString("点击这里跳转")
spannable.setSpan(object : ClickableSpan() {
    override fun onClick(widget: View) {
        // 点击事件
    }
}, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
textView.text = spannable

二、Button #

Button是可点击的按钮控件。

2.1 基本用法 #

xml
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="点击我"
    android:onClick="onButtonClick" />
kotlin
// 方式1:XML中指定onClick
fun onButtonClick(view: View) {
    Toast.makeText(this, "按钮被点击", Toast.LENGTH_SHORT).show()
}

// 方式2:代码设置监听器
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
    Toast.makeText(this, "按钮被点击", Toast.LENGTH_SHORT).show()
}

2.2 按钮样式 #

xml
<!-- Material按钮样式 -->
<Button
    style="@style/Widget.MaterialComponents.Button"
    android:text="Filled" />

<Button
    style="@style/Widget.MaterialComponents.Button.OutlinedButton"
    android:text="Outlined" />

<Button
    style="@style/Widget.MaterialComponents.Button.TextButton"
    android:text="Text" />

<Button
    style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
    android:text="Unelevated" />

2.3 图标按钮 #

xml
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="分享"
    android:drawableLeft="@drawable/ic_share"
    android:drawablePadding="8dp" />

<!-- Material图标按钮 -->
<com.google.android.material.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="分享"
    app:icon="@drawable/ic_share"
    app:iconGravity="textStart" />

2.4 ImageButton #

xml
<ImageButton
    android:id="@+id/imageButton"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:src="@drawable/ic_share"
    android:background="?attr/selectableItemBackgroundBorderless"
    android:contentDescription="分享" />

三、EditText #

EditText是可编辑的文本输入框。

3.1 基本用法 #

xml
<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入内容"
    android:inputType="text" />

3.2 常用属性 #

属性 说明
android:hint 提示文字
android:inputType 输入类型
android:maxLines 最大行数
android:minLines 最小行数
android:maxLength 最大字符数
android:singleLine 单行模式
android:selectAllOnFocus 获取焦点时全选
android:imeOptions 软键盘动作按钮

3.3 输入类型 #

类型 说明
text 普通文本
textPassword 密码
textEmailAddress 邮箱
textMultiLine 多行文本
number 数字
numberDecimal 小数
phone 电话号码
textUri URL
xml
<EditText
    android:inputType="textPassword" />

<EditText
    android:inputType="textEmailAddress" />

<EditText
    android:inputType="number" />

3.4 监听文本变化 #

kotlin
val editText = findViewById<EditText>(R.id.editText)

editText.addTextChangedListener(object : TextWatcher {
    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        // 文本变化前
    }
    
    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        // 文本变化中
    }
    
    override fun afterTextChanged(s: Editable?) {
        // 文本变化后
        Log.d("EditText", "输入内容: ${s.toString()}")
    }
})

3.5 软键盘动作 #

xml
<EditText
    android:imeOptions="actionSearch"
    android:inputType="text" />
kotlin
editText.setOnEditorActionListener { v, actionId, event ->
    if (actionId == EditorInfo.IME_ACTION_SEARCH) {
        // 执行搜索
        performSearch()
        true
    } else {
        false
    }
}

3.6 TextInputLayout #

Material Design风格的输入框:

xml
<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="用户名"
    app:endIconMode="clear_text">
    
    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text" />
        
</com.google.android.material.textfield.TextInputLayout>

<!-- 密码输入框 -->
<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="密码"
    app:endIconMode="password_toggle">
    
    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword" />
        
</com.google.android.material.textfield.TextInputLayout>

四、ImageView #

ImageView用于显示图片。

4.1 基本用法 #

xml
<ImageView
    android:id="@+id/imageView"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:src="@drawable/image"
    android:contentDescription="图片描述" />

4.2 常用属性 #

属性 说明
android:src 图片资源
android:scaleType 缩放类型
android:adjustViewBounds 调整边界保持比例
android:maxWidth 最大宽度
android:maxHeight 最大高度
android:tint 着色
android:cropToPadding 是否裁剪到padding

4.3 缩放类型 #

类型 说明
matrix 使用matrix矩阵绘制
fitXY 拉伸填充
fitStart 保持比例,左上对齐
fitCenter 保持比例,居中
fitEnd 保持比例,右下对齐
center 居中显示,不缩放
centerCrop 保持比例缩放,裁剪填充
centerInside 保持比例缩放,完整显示
xml
<ImageView
    android:scaleType="centerCrop" />

<ImageView
    android:scaleType="fitCenter" />

<ImageView
    android:scaleType="centerInside" />

4.4 加载网络图片 #

使用Glide加载网络图片:

kotlin
Glide.with(this)
    .load("https://example.com/image.jpg")
    .placeholder(R.drawable.placeholder)
    .error(R.drawable.error)
    .into(imageView)

使用Coil加载网络图片:

kotlin
imageView.load("https://example.com/image.jpg") {
    placeholder(R.drawable.placeholder)
    error(R.drawable.error)
    transformations(CircleCropTransformation())
}

4.5 圆形图片 #

xml
<!-- 使用ShapeableImageView -->
<com.google.android.material.imageview.ShapeableImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/avatar"
    app:shapeAppearanceOverlay="@style/CircleImage" />
xml
<style name="CircleImage">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
</style>

五、其他常用控件 #

5.1 CheckBox #

xml
<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="记住密码" />
kotlin
checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
    if (isChecked) {
        // 选中
    } else {
        // 取消选中
    }
}

5.2 RadioButton #

xml
<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    
    <RadioButton
        android:id="@+id/rbMale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="男" />
    
    <RadioButton
        android:id="@+id/rbFemale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="女" />
        
</RadioGroup>
kotlin
radioGroup.setOnCheckedChangeListener { group, checkedId ->
    when (checkedId) {
        R.id.rbMale -> { }
        R.id.rbFemale -> { }
    }
}

5.3 Switch #

xml
<Switch
    android:id="@+id/switch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="开关" />
kotlin
switch.setOnCheckedChangeListener { buttonView, isChecked ->
    // 处理开关状态
}

5.4 ProgressBar #

xml
<!-- 圆形进度条 -->
<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<!-- 水平进度条 -->
<ProgressBar
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:progress="30" />

5.5 SeekBar #

xml
<SeekBar
    android:id="@+id/seekBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:progress="50" />
kotlin
seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
    override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
        // 进度变化
    }
    
    override fun onStartTrackingTouch(seekBar: SeekBar?) {
        // 开始拖动
    }
    
    override fun onStopTrackingTouch(seekBar: SeekBar?) {
        // 停止拖动
    }
})

六、总结 #

本章详细介绍了Android常用控件:

  1. TextView文本显示控件
  2. Button按钮控件
  3. EditText输入框控件
  4. ImageView图片显示控件
  5. 其他常用控件

掌握这些控件的使用是Android UI开发的基础。

最后更新:2026-03-26