安卓常用控件(上)

文章目录

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 设置字与字的垂直间隔。
相关推荐
阿珊和她的猫3 小时前
v-scale-scree: 根据屏幕尺寸缩放内容
开发语言·前端·javascript
_Kayo_5 小时前
node.js 学习笔记3 HTTP
笔记·学习
fouryears_234175 小时前
Flutter InheritedWidget 详解:从生命周期到数据流动的完整解析
开发语言·flutter·客户端·dart
我好喜欢你~6 小时前
C#---StopWatch类
开发语言·c#
桦说编程7 小时前
Java 中如何创建不可变类型
java·后端·函数式编程
lifallen7 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研7 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
CCCC13101638 小时前
嵌入式学习(day 28)线程
jvm·学习
没有bug.的程序员8 小时前
JVM 总览与运行原理:深入Java虚拟机的核心引擎
java·jvm·python·虚拟机
甄超锋8 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat