安卓常用控件(上)

文章目录

TextView

textview主要用于在界面上显示一段文本信息。

属性名 描述
id 给当前控件定义一个唯一的标识符。
layout_width 给控件指定一个宽度。match_parent:控件大小与父布局一样;wrap_content:控件大小刚好够包含住内容固定值表示表示给控件指定一个固定的尺寸,单位一般用dp。
layout_height 给控件指定一个高度,可选值和layout_width一样。
text 显示文本内容。
gravity 指定文字的对齐方式,可选值有top、bottom、start、end、center等,可以用"|"来同时指定多个值。
textColor 指定文字的颜色。
textSize 指定文字的大小。文字大小使用sp作为单位,这样当用户在系统中修改了文字显示尺寸时,应用程序中的文字大小也会跟着变化。
textStyle 设置字体风格,normal(无效果),bold(加粗),italic(斜体)。
background 控件的背景颜色,可以理解为填充整个控件的颜色,可以是图片。
shadowRadius 设置阴影的模糊程度,设为0.1就变成字体颜色了,建议使用3.0。
shadowColor 设置阴影颜色,需要与shadowRadius一起使用。
shadowDx 设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置。
shadowDy 设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置。
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/text_view1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="TextView1"
        android:gravity="right|bottom"
        android:textColor="#f20e33"
        android:textSize="30sp"
        android:textStyle="bold|italic"
        android:background="#d1e4eb"
        android:shadowRadius="3.0"
        android:shadowColor="#ffe040"
        android:shadowDx="15.0"
        android:shadowDy="10.0" />
</LinearLayout>

Button

button是程序用于和用户进行交互的一个重要控件。

属性名 描述
id 给当前控件定义一个唯一的标识符。
layout_width 给控件指定一个宽度。match_parent:控件大小与父布局一样;wrap_content:控件大小刚好够包含住内容固定值表示表示给控件指定一个固定的尺寸,单位一般用dp。
layout_height 给控件指定一个高度,可选值和layout_width一样。
text 显示文本内容。
textAllCaps Android系统默认会将按钮上的英文字母全部转换成大写,取值为false时,系统就不会改变大小写。
gravity 指定文字的对齐方式,可选值有top、bottom、start、end、center等,可以用"|"来同时指定多个值。
textColor 指定文字的颜色。
textSize 指定文字的大小。文字大小使用sp作为单位,这样当用户在系统中修改了文字显示尺寸时,应用程序中的文字大小也会跟着变化。
textStyle 设置字体风格,normal(无效果),bold(加粗),italic(斜体)。
background 控件的背景颜色,可以理解为填充整个控件的颜色,可以是图片。
shadowRadius 设置阴影的模糊程度,设为0.1就变成字体颜色了,建议使用3.0。
shadowColor 设置阴影颜色,需要与shadowRadius一起使用。
shadowDx 设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置。
shadowDy 设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置。

Android Studio 4.1之后的版本进行开发时,创建的项目默认的主题都是Theme.MaterialComponents.DayNight.DarkActionBar。所有Button都是Material类型的Button,默认使用主题色。需要打开res\values\themes.xml文件修改< style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">为 < style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:gravity="right|bottom"
        android:textAllCaps="false"
        android:textColor="#f20e33"
        android:textSize="30sp"
        android:textStyle="normal"
        android:shadowRadius="3.0"
        android:shadowColor="#ffe040"
        android:shadowDx="10.0"
        android:shadowDy="10.0"
        android:background="#d1e4eb"/>
</LinearLayout>

EditText

EditText是程序用于和用户进行交互的另一个重要控件,它允许用户在控件里输入和编辑内容,并可以在程序中对这些内容进行处理。

属性名 描述
id 给当前控件定义一个唯一的标识符。
layout_width 给控件指定一个宽度。match_parent:控件大小与父布局一样;wrap_content:控件大小刚好够包含住内容固定值表示表示给控件指定一个固定的尺寸,单位一般用dp。
layout_height 给控件指定一个高度,可选值和layout_width一样。
hint 输入框提示内容。
inputType 指定类型。
textColor 指定文字的颜色。
textSize 指定文字的大小。文字大小使用sp作为单位,这样当用户在系统中修改了文字显示尺寸时,应用程序中的文字大小也会跟着变化。
maxLines edittext会根据输入内容多少改变高度从而显示完所有行数,maxLines可以指定最大行数。
minLines 指定最小行数。
singleLine 是否限制editText只允许单行输入。
textScaleX 设置字与字的水平间隔。
textScaleY 设置字与字的垂直间隔。
相关推荐
JH30732 小时前
SpringBoot 优雅处理金额格式化:拦截器+自定义注解方案
java·spring boot·spring
盐焗西兰花3 小时前
鸿蒙学习实战之路-Reader Kit修改翻页方式字体大小及行间距最佳实践
学习·华为·harmonyos
2501_916008893 小时前
全面介绍Fiddler、Wireshark、HttpWatch、SmartSniff和firebug抓包工具功能与使用
android·ios·小程序·https·uni-app·iphone·webview
QiZhang | UESTC3 小时前
学习日记day76
学习
久邦科技3 小时前
20个免费电子书下载网站,实现电子书自由(2025持续更新)
学习
m0_736919103 小时前
C++代码风格检查工具
开发语言·c++·算法
Coder_Boy_3 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
2501_944934733 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
Gain_chance3 小时前
34-学习笔记尚硅谷数仓搭建-DWS层最近一日汇总表建表语句汇总
数据仓库·hive·笔记·学习·datagrip
玉梅小洋3 小时前
Windows 10 Android 构建配置指南
android·windows