安卓常用控件(上)

文章目录

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 设置字与字的垂直间隔。
相关推荐
Dontla几秒前
冒烟测试介绍(Smoke Test、BVT构建验证测试、构建验收测试)与回归测试对比、与单元测试对比、与集成测试对比、Cypress
java·单元测试·集成测试
名字还没想好☜20 分钟前
Go 结构体内存对齐:调整字段顺序,同样的字段省下 40% 内存
开发语言·后端·golang·go·内存对齐
暖和_白开水22 分钟前
数据分析agent(十):loguru日志输出优化 和es 启动流程
java·elasticsearch·数据分析
减瓦22 分钟前
Gradle 版本演进全景:构建工具的进化密码
java·gradle
玛丽莲茼蒿22 分钟前
很难出错的Spring5(十)—— IOC进阶
java·开发语言·jvm
Ulyanov23 分钟前
雷达导引头仿真中的坐标系——从惯性系到量测角
开发语言·python·算法·系统仿真·雷达信号处理·雷达导引头
减瓦27 分钟前
Jackson 使用指南
java·spring boot·json
Devin~Y30 分钟前
互联网大厂 Java 面试实录:Spring Boot、MyBatis、Redis、Kafka、Spring Security、RAG 与 MCP 全链路问答
java·redis·kafka·mybatis·spring security·spring mvc·sprint boot
weixin_4376629832 分钟前
redisson
java
码农进化录34 分钟前
Java 程序员的 AI 进化论 | AI 加 Postman 跑接口测试,省了三天活
java·后端·openai