Android开发常用控件
时间: 2025-04-29 13:55:58 浏览: 12
### Android 开发常用 UI 控件及其使用方法
#### TextView
`TextView` 是用于显示文本信息的基础控件。可以设置其宽高、对齐方式以及字体大小等属性。例如,通过 `android:gravity="right"` 可以让文字右对齐;而 `android:textSize="25sp"` 则指定了文字尺寸为 25 sp[^2]。
```xml
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="25sp"
android:textColor="#00ff00"
android:text="你好"/>
```
#### Button
按钮是交互界面上不可或缺的一部分,点击事件通常绑定在此类组件上触发特定功能逻辑。除了基本样式外,还可以自定义背景颜色或者图标来增强视觉效果。
```xml
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!" />
```
#### Switch
开关控件允许用户开启或关闭某个选项状态,在表单设计里非常实用。监听器能够捕捉到切换动作并作出响应处理[^4]。
```java
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Handle the state change here.
}
});
```
#### EditText
编辑框让用户输入数据,支持多行模式和密码隐藏等功能特性。可以通过设置提示语句引导用户提供有效信息[^1]。
```xml
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name..." />
```
#### ImageView
图像视图负责展示图片资源文件,无论是静态还是动态加载都可以很好地兼容。还提供了缩放裁剪等多种操作手段满足不同场景下的需求。
```xml
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sample_image" />
```
#### CheckBox
复选框允许多项选择,适用于问卷调查或是权限配置之类的应用场合。同样具备改变外观样式的灵活性以便更好地融入整体风格之中。
```xml
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember me?" />
```
#### RadioButton 和 RadioGroup
二者配合实现单项选择机制,前者代表每一个独立的选择项后者则是容器用来管理这些子元素之间的关系确保每次仅有一个被激活。
```xml
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/optionOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option One" />
<RadioButton
android:id="@+id/optionTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option Two" />
</RadioGroup>
```
阅读全文
相关推荐

















