安卓常用控件(上)

文章目录

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 设置字与字的垂直间隔。
相关推荐
ace望世界10 分钟前
android的Parcelable
android
用户03321266636711 分钟前
Java 查找并替换 Excel 中的数据:详细教程
java
顾林海12 分钟前
Android编译插桩之AspectJ:让代码像特工一样悄悄干活
android·面试·性能优化
间彧13 分钟前
ThreadLocal实现原理与应用实践
java
若水不如远方20 分钟前
Netty的四种零拷贝机制:深入原理与实战指南
java·netty
叽哥23 分钟前
Flutter Riverpod上手指南
android·flutter·ios
用户74936368484325 分钟前
【开箱即用】一分钟使用java对接海外大模型gpt等对话模型,实现打字机效果
java
循环不息优化不止27 分钟前
安卓开发设计模式全解析
android
诺诺Okami28 分钟前
Android Framework-WMS-层级结构树
android
SimonKing44 分钟前
一键开启!Spring Boot 的这些「魔法开关」@Enable*,你用对了吗?
java·后端·程序员