• 第四篇Android--TextView使用详解


    TextView是View体系中的一员,继承自View,用于在界面中展示文字。

    基本用法:

    1. <TextView
    2. android:id="@+id/textview"
    3. android:layout_width="wrap_content"
    4. android:layout_height="wrap_content"
    5. android:padding="10dp"
    6. android:text="Hello World!"
    7. android:textSize="16sp" />

    1.  设置点击事件:

    1. findViewById(R.id.textview).setOnClickListener(v -> {
    2. Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_SHORT).show();
    3. });

    2.设置文字颜色1):

     android:textColor="@color/black"

    上面方式设置的颜色只有一种状态。如果需要TextView展示时一种颜色,按下时展示另外一种颜色,可以通过设置selector实现。

       设置文字颜色2):

      在res目录下创建color文件夹,在次文件夹下,可以创建颜色选择器selector文件。

     

    text_selector.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
    3. <item android:color="@android:color/holo_red_light" android:state_pressed="true" />
    4. <item android:color="@android:color/holo_red_dark" />
    5. </selector>

    颜色选择器使用:正常显示一种颜色,按下显示另外一种颜色。

     3.设置背景色

        1)可以是颜色值:

     android:background="@color/green"

       2)也可以是drawable

     android:background="@drawable/ic_launcher_background"

      3)以上两种方式只能设置单一背景色,通过selector可以设置按下时的颜色。

          在drawable目录下创建xml文件

         

    text_bg.xml 

     需要注意的是:需要引用drawable,需要设置state_press的值,false正常显示,true按下显示

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
    3. <item android:drawable="@drawable/color_normal" android:state_pressed="false"/>
    4. <item android:drawable="@drawable/color_press" android:state_pressed="true"/>
    5. </selector>

      在color.xml定义drawable

    1. <drawable name="color_normal">#eeeeee</drawable>
    2. <drawable name="color_press">#6592f6</drawable>

    TextView中引入:

    1. <TextView
    2. android:id="@+id/textview"
    3. android:layout_width="wrap_content"
    4. android:layout_height="wrap_content"
    5. android:padding="10dp"
    6. android:textColor="@color/text_selector"
    7. android:text="Hello World!"
    8. android:background="@drawable/text_bg"
    9. android:textSize="16sp" />

    4. autoLink 自动链接

        当输入的是手机号时,autoLink设置为phone。点击文本时会自动跳转到拨号界面。

    1. android:text="10086"
    2. android:autoLink="phone"

      邮箱格式:会自动调整到邮箱服务界面:

    1. android:text="10086@qq.com"
    2. android:autoLink="email"

      超链接:会自动打开系统的浏览器访问该网址

    1. android:text="www.baidu.com"
    2. android:autoLink="web"

    5. android:gravity。设置文本显示格式,居左,居中居右等。

    1. android:gravity="left"
    2. android:gravity="center"
    3. android:gravity="right"
  • 相关阅读:
    leetcode/复原 IP
    spring中有哪几种加载ApplicationContext.xml的方式呢?
    搞定“项目八怪”,你就是管理高手!
    vue http页面新开窗口跳转https页面
    基于柔性人机接口的人机协调运动控制方法
    [附源码]计算机毕业设计基于springboot的小区宠物管理系统
    一键生成ios&android应用图标
    LeetCode: 406. 根据身高重建队列
    【Java面试】这道互联网高频面试题难住了80%的程序员?索引什么时候失效?
    2022/7/30周总结
  • 原文地址:https://blog.csdn.net/niuyongzhi/article/details/133787299