文本类控件
- 几个像素相关单位
- px::pixels(就是物理像素)
- ppi:就是通过那个公式计算出来的物理像素密度
- dpi:参考了物理像素密度后人为指定的一个值,这样使得某一个区间内的物理像素密度都是相同的dpi
- dp:device independent pixels(设备独立像素),安卓中尺寸设置一般就用dp;dp 和 px 之间的换算关系是,设备是 160dpi 的话,1dp就等于1px,如果设备的dpi是320的话,1dp就等于2px;其实使用dp也不能做到完美适配所有的屏幕,要想很好适配尽可能多的设备比较好的方案就是使用
ConstraintLayout
布局,具体到尺寸和布局的话就使用百分比而不写死dp,这样屏幕的适配就比较完美了 - sp:scaled pixels(放大像素),用于设置字体的大小
AS使用的一个小技巧:设置颜色的时候点击前面的显示颜色的方块可以唤出取色盘
一、TextView(文本框)
1、基础属性
- 直接看示例代码,基本就是见名知义,没啥好说
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center"
android:background="#8fffad">
<TextView
android:id="@+id/txtOne"
android:layout_width="200dp"
android:layout_height="200dp"
android:gravity="center"
android:text="TextView(显示框)"
android:textColor="#EA5246"
android:textStyle="bold|italic"
android:background="#000000"
android:textSize="18sp" />
</RelativeLayout>
2、特殊效果实现
字间距、行间距、自动换行
- 字间距的设置
android:textScaleX:控制字体水平方向的缩放,默认值1.0f,值是float
Java中setScaleX(2.0f);
- 行间距的设置
android:lineSpacingExtra:设置行间距,如"3dp"
android:lineSpacingMultiplier:设置行间距的倍数,如"1.2"
Java代码中可以通过: setLineSpacing方法来设置
- 自动换行
android:singleLine = "true" 设置为true 就是一行显示,false就是会自动换行
阴影效果
- android:shadowColor:设置阴影颜色,需要与shadowRadius一起使用哦!
- android:shadowRadius:设置阴影的模糊程度,设为0.1就变成字体颜色了,建议使用3.0
- android:shadowDx:设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置
- android:shadowDy:设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:shadowColor="#F9F900"
android:shadowDx="10.0"
android:shadowDy="10.0"
android:shadowRadius="3.0"
android:text="带阴影的TextView"
android:textColor="#4A4AFF"
android:textSize="30sp" />
AS里面预览的界面是看不到阴影的效果的,需要将应用运行起来才能看到阴影
添加边框(ShapeDrawable资源文件)
- shapeDrawable文件的详细说明
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither=["true" | "false"] //将在位图的像素配置与屏幕不同时(例如:ARGB 8888 位图和 RGB 565 屏幕)启用位图的抖动;值为“false”时则停用抖动。默认值为 true。
android:shape=["rectangle" | "oval" | "line" | "ring"]//分别为矩形、椭圆、线、环。默认为矩形rectangle
android:innerRadius="integer" // shape为ring时有效,内环半径
android:innerRadiusRatio="float" // shape为ring时有效,内环的厚度比,即环的图形宽度与内环半径的比例,按照这个比例计算内环半径,默认为3,可被innerRadius值覆盖
android:thickness="integer" // shape为ring时有效,环的厚度
android:thicknessRatio="float" // shape为ring时有效,环的厚度比,即环的图形宽度与环的厚度的比例,按照这个比例计算环的厚度,默认为9,可被thickness值覆盖
android:tint="color" // 给shape着色
android:tintMode=["src_in" | "src_atop" | "src_over" | "add" | "multiply" | "screen"] // 着色类型
android:useLevel=["true" | "false"] // 较少用,一般设为false,否则图形不显示。为true时可在LevelListDrawable使用
android:visible=["true" | "false"] >
<!-- 圆角 -->
<corners
android:radius="integer" // 圆角半径,设置下面四个属性时,对应的位置属性会被覆盖
android:topLeftRadius="integer" // 左上角圆角半径
android:topRightRadius="integer" // 右上角圆角半径
android:bottomLeftRadius="integer" // 左下角圆角半径
android:bottomRightRadius="integer" // 右下角圆角半径
/>
<!-- 渐变 -->
<gradient
android:type=["linear" | "radial" | "sweep"]// 渐变类型,线性、放射性、扫描性;默认为线性
android:angle="integer" // 渐变角度,渐变类型为linear时有效;默认为0,从左至右渐变,角度逆时针方向计算,角度需要时45的整数倍数
android:centerColor="integer" // 渐变中间位置颜色
android:startColor="color" // 渐变开始位置颜色
android:endColor="color" // 渐变结束位置颜色
android:centerX="float" // 设置渐变中心的X坐标,取值区间[0,1],默认为0.5,即中心位置
android:centerY="float" // 设置渐变中心的Y坐标,取值区间[0,1],默认为0.5,即中心位置
android:gradientRadius="integer" // type为放射性渐变radial时有效,渐变的半径
android:useLevel=["true" | "false"] // 与shape中该属性的一致
/>
<!-- 内边距 -->
<padding
android:left="integer" // 左边距
android:top="integer" // 上边距
android:right="integer" // 右边距
android:bottom="integer" // 下边距
/>
<!-- 大小 -->
<size
android:width="integer" // 图形宽度
android:height="integer" // 图形高度
/>
<!-- 填充 -->
<solid
android:color="color" // 图形的填充色
/>
<!-- 描边 -->
<stroke
android:width="integer" // 描边的宽度
android:color="color" // 描边的颜色
android:dashWidth="integer" // 虚线宽度
android:dashGap="integer" // 虚线间隔
/>
</shape>
- 圆角矩形边框的Drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#87CEEB" />
<stroke
android:width="1px"
android:color="#000000" />
<corners
android:bottomLeftRadius="10px"
android:bottomRightRadius="10px"
android:topLeftRadius="10px"
android:topRightRadius="10px" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
- 设置背景为相应shapeDrawable文件
<TextView
android:id="@+id/txtTwo"
android:layout_width="200dp"
android:layout_height="64dp"
android:layout_marginTop="10dp"
android:textSize="18sp"
android:gravity="center"
android:background="@drawable/txt_radiuborder"
android:text="圆角边框的TextView" />
带图片的TextView
- drawableXXX分别设置上下左右的图片
- 使用drawablePadding来设置图片与文字间的间距
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:drawableTop="@drawable/fg"
android:drawableLeft="@drawable/fg"
android:drawableRight="@drawable/fg"
android:drawableBottom="@drawable/fg"
android:drawablePadding="50dp"
android:text="跑动的妹纸" />
- 图片的大小只能通过 java 代码调节
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.tv1);
Drawable[] drawable = text