| 本帖最后由 liu 于 2018-12-1 12:50 编辑 
 TextView
 
 
  
 
 文本框,用于显示文本的一个控件 1、一些常用属性:
 
 id                       为TextView设置一个id,在Java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置
 
 layout_width        TextView的宽度
 layout_height       TextView的高度                 
 gravity                 TextView中内容的对齐方向text                     设置显示的文本内容
 
 textColor              设置字体颜色textStyle               设置字体风格,可设置的值:normal(无效果),bold(加粗),italic(斜体)
 
 textSize                字体大小,单位sp
 background           控件的背景颜色
 示例代码:
 
 [XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context="com.example.asus.myapplication.MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="150dp"
        android:layout_height="60dp"
        android:background="@android:color/holo_red_dark"
        android:gravity="center"
        android:text="Hello World!"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:textStyle="bold" />
</LinearLayout>
效果图:
 
 
   
 2、自定义TextView的样式
 
 <solidandroid:color = "xxx">                                     设置背景颜色
 <stroke android:width = "xdp" android:color="xxx">     设置边框宽度和颜色
 <padding androidLbottom = "xdp">                             设置边距
 <corners android:topLeftRadius="5px">                       设置圆角
 <gradient>                                                                设置渐变色,可选属性有:startColor:起始颜色  endColor:结束颜色  centerColor:中间颜色  angle:方向角度  type:设置渐变的类型 
 使用方法:在res-drawable下新建一个xml文件,在其中设置属性,然后将此xml文件设置为TextView的背景 
 
 
 
 示例代码:
 bg_textview.xml:
              [XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="50dp" />
    <solid android:color="@color/colorAccent" />
    <stroke
        android:width="5dp"
        android:color="@android:color/black" />
    
</shape> 
 activity_main.xml:
 [XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context="com.example.asus.myapplication.MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/bg_textview"
        android:gravity="center"
        android:text="Hello World!"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:textStyle="bold" />
</LinearLayout>
效果图: 
   
 
 3、带阴影的TextView
 
 android:shadowColor                     设置阴影颜色,需要与shadowRadius一起使用
 
         android:shadowRadius                  设置阴影的模糊程度
         android:shadowDx                          设置阴影在水平方向的偏移
         android:shadowDy                          设置阴影在竖直方向的偏移 
 示例代码:
 
 [XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context="com.example.asus.myapplication.MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:gravity="center"
        android:text="Hello World!"
        android:textSize="20sp"
        android:shadowColor="@color/colorAccent"
        android:shadowRadius="3.0"
        android:shadowDx="10.0"
        android:shadowDy="10.0"/>
</LinearLayout>
效果图:
 
 
   
 
 4、设置最多显示的字符数,剩余显示省略号
 
 示例代码
 
 [XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context="com.example.asus.myapplication.MainActivity">
    <!-- android:ellipsize省略号显示位置  android:maxLines最大行数  android:maxEms设置可显示的最多字符 -->
    <TextView
        android:id="@+id/textView"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:maxEms="10"
        android:text="Hello World!Hello World!Hello World!"
        android:textSize="20sp" />
</LinearLayout>
 
 效果图下图:
 
 
   
 
 还有些功能没说到,之后会补上
 
 
 
 
 
                          
 
 
 |