2.02TextView(文本框)_UI组件_Android

TOC

TextView文本框_UI组件_Android

  • 文本的字体尺寸单位为 sp :
  • sp: scaled pixels(放大像素). 主要用于字体显示。

属性

属性名称 属性含义
id 为TextView设置一个组件id,根据id,我们可以在Java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置
layout_width 组件的宽度
layout_height 组件的高度
gravity 设置控件中内容的对齐方向,TextView中是文字,ImageView中是图片等等
text 设置显示的文本内容,一般我们是把字符串写到string.xml文件中,然后通过@String/xxx取得对应的字符串内容的
textColor 设置字体颜色,同上,通过colors.xml资源来引用
textStyle 设置字体风格,三个可选值:normal(无效果),bold(加粗),italic(斜体)
textSize 字体大小,单位一般是用sp
background 控件的背景颜色,可以理解为填充整个控件的颜色,可以是图片
autoLink 识别链接类型 (web, email, phone ,map ,none, all)

写文本的那直接使用不会直接使用字符串,通常先会定义一个常量,然后引用
res/values/strings.xml中定义字符串

定义的字符串:<string name="app_name">android_learn1</string>
在xml中引用:
    <!--@string引入字符串
    @color引入颜色
    @mipmap引入图片-->
    <TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher"
        android:gravity="right"
        android:text="@string/app_name"
        android:textColor="@color/colorAccent" />

    <EditText
        android:id="@+id/edit_username"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:hint="请输入用户名">

TextView设置边框

编写一个ShapeDrawable的资源文件,然后TextView将 background 设置为这个drawable 资源即可
2.03Shape(形状)_Android
例如:

<solid android:color = "xxx"> 这个是设置背景颜色的
<stroke android:width = "xdp" android:color="xxx"> 这个是设置边框的粗细,以及边框颜色的
<padding androidLbottom = "xdp"...> 这个是设置边距的
<corners android:topLeftRadius="10px"...> 这个是设置圆角的
<gradient> 这个是设置渐变色的,可选属性有: startColor:起始颜色 endColor:结束颜色 centerColor:中间颜色 angle:方向角度,等于0时,从左到右,然后逆时针方向转,当angle = 90度时从下往上 type:设置渐变的类型

矩形边框的Drawable

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 设置一个黑色边框,宽度2px ,颜色黑色 -->
    <stroke android:width="2px" android:color="#000000"/>
    <!-- 渐变,角度,开始颜色,结束颜色 -->
    <gradient
        android:angle="270"
        android:endColor="#C0C0C0"
        android:startColor="#FCD209" />
    <!-- 设置一下边距,让空间大一点 -->
    <padding
        android:left="5dp"
        android:top="5dp"
        android:right="5dp"
        android:bottom="5dp"/>

</shape>


在组件中引用

将TextView背景设置成编写的Drawable
<TextView
    android:layout_width="300dp"
    android:layout_height="100dp"
    android:text="带边框的TextView"
    android:layout_below="@+id/textview1"
    android:gravity="center"
    android:layout_centerHorizontal="true"
    android:textSize="30dp"
    android:background="@drawable/solidtextview"/>

圆角矩形边框的Drawable

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <!-- 设置透明背景色 -->
    <solid android:color="#87CEEB" />
    <!-- 设置一个黑色边框 -->
    <stroke
        android:width="2px"
        android:color="#000000" />
    <!-- 设置四个圆角的半径 -->
    <corners
        android:bottomLeftRadius="100px"
        android:bottomRightRadius="10px"
        android:topLeftRadius="10px"
        android:topRightRadius="10px" />
    <!-- 设置一下边距,让空间大一点 -->
    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />
</shape>

带图片(drawableXxx)的TextView

属性名 作用
android:drawableLeft 文本左边设置图片
android:drawableRight 文本右边设置图片
android:drawableBottom 文本下边设置图片
android:drawableTop 文本上边设置图片

应用场景

属性使用

<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="com.jay.example.test.MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:drawableTop="@drawable/show1"
        android:drawableLeft="@drawable/show1"
        android:drawableRight="@drawable/show1"
        android:drawableBottom="@drawable/show1"
        android:drawablePadding="10dp"
        android:text="张全蛋" />
</RelativeLayout>

原文地址:https://www.cnblogs.com/ziyue7575/p/16dd3be546729764262a53ad7749aa80.html