安卓常用控件(上)

文章目录

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 设置字与字的垂直间隔。
相关推荐
^Rocky几秒前
JavaScript性能优化实战
开发语言·javascript·性能优化
田里的水稻8 分钟前
C++_队列编码实例,从末端添加对象,同时把头部的对象剔除掉,中的队列长度为设置长度NUM_OBJ
java·c++·算法
Hello_Embed15 分钟前
STM32HAL 快速入门(十九):UART 编程(二)—— 中断方式实现收发及局限分析
笔记·stm32·单片机·嵌入式硬件·学习
ponnylv18 分钟前
深入剖析Spring Boot启动流程
java·开发语言·spring boot·spring
萧邀人24 分钟前
第一课、Cocos Creator 3.8 安装与配置
开发语言
天上的光32 分钟前
关于学习的一些感悟
学习
前行的小黑炭34 分钟前
Android 协程的使用:结合一个环境噪音检查功能的例子来玩玩
android·java·kotlin
阿华的代码王国43 分钟前
【Android】内外部存储的读写
android·内外存储的读写
李少兄1 小时前
解决IntelliJ IDEA 提交代码时无复选框问题
java·ide·intellij-idea
Jayden_Ruan1 小时前
C++逆向输出一个字符串(三)
开发语言·c++·算法