Android开发中TextView各种常见使用方法小结

 更新时间:2019年04月09日 11:00:56   作者:水中鱼之1999  
这篇文章主要介绍了Android开发中TextView各种常见使用方法,结合实例形式总结分析了Android开发中TextView各种常见布局与功能实现技巧,需要的朋友可以参考下

本文实例讲述了Android开发中TextView各种常见使用方法。分享给大家供大家参考,具体如下:

效果图:

XML布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  android:id="@+id/root"
  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:orientation="vertical">
  <!--设置字号20sp,文本框结尾处绘制图片-->
  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="我爱Java"
    android:textSize="20pt"
    android:drawableRight="@drawable/ic_dashboard_black_24dp" />
  <!--设置中间省略,所有字母大写-->
  <!--android:ellipsize="middle" ···省略号居中显示-->
  <!--android:textAllCaps="true"全体大写-->
  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:text="我爱Java我爱Java我爱Java我爱Java我爱Java我爱Java我爱Java我爱Java"
    android:ellipsize="middle"
    android:textAllCaps="true"/>
  <!--对邮件电话、添加链接-->
  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:text="邮箱是 814484626@qq.com, 电话是 13100000000"
    android:autoLink="email|phone"/>
  <!--设置颜色、大小、并使用阴影-->
  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="测试文字"
    android:textSize="25pt"
    android:shadowColor="#00f"
    android:shadowDx="10.0"
    android:shadowDy="8.0"
    android:shadowRadius="3.0"
    android:textColor="#f00"/>
  <!--测试密码框-->
  <TextView
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    android:textSize="25sp"
    android:password="true"/>
  <!--测试密码框-->
  <CheckedTextView
    android:id="@+id/check_text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="可勾选的文本"
    android:textSize="25sp"
    android:checkMark="@xml/check"/>
  <!--通过Android:background指定背景-->
  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="带边框的文本"
    android:textSize="25sp"
    android:background="@drawable/bg_border"/>
  <!--通过Android:drawableLeft绘制一张图片-->
  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="圆角边框,渐变背景的文本"
    android:textSize="25sp"
    android:background="@drawable/bg_border2"/>
</LinearLayout>

bg_bordor

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <!--设置背景色为透明色-->
  <solid android:color="#0000"/>
  <!--设置红色边框-->
  <stroke android:width="4px" android:color="#f00"/>
</shape>

bg_bordor2

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
  <!--制定圆角矩形的四个圆角半径-->
  <corners android:topLeftRadius="30px"
    android:topRightRadius="5px"
    android:bottomRightRadius="30px"
    android:bottomLeftRadius="5px"/>
  <!--指定边框线条的宽度和颜色-->
  <stroke android:width="4px" android:color="#f0f"/>
  <!--指定使用渐变背景色,使用sweep类型渐变
  颜色从 红色->绿色->蓝色-->
  <gradient android:startColor="#f00"
    android:centerColor="#0f0"
    android:endColor="#00f"
    android:type="sweep"/>
</shape>

勾选效果通过xml selector实现切换

android:checkMark="@xml/check"/>

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/ok" android:state_selected="true"/>
  <item android:drawable="@drawable/no" android:state_selected="false"/>
</selector>

Java代码添加点击事件

public class Home extends AppCompatActivity {
  private int i = 0 ;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);//显示manLayout
    final CheckedTextView checkedTextView = (CheckedTextView) findViewById(R.id.check_text_view);
    checkedTextView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if( i++ % 2 == 1 ){
          checkedTextView.setSelected(true);
        }
        else {
          checkedTextView.setSelected(false);
        }
      }
    });
  }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • Android中Socket大文件断点上传示例

    Android中Socket大文件断点上传示例

    本篇文章主要介绍了Android中Socket大文件断点上传示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • Android开发仿咸鱼键盘DEMO(修改版)

    Android开发仿咸鱼键盘DEMO(修改版)

    本文给大家分享一段代码关于android开发高仿咸鱼键盘修改版的实例代码,代码简单易懂,非常实用,需要的朋友参考下吧
    2016-11-11
  • Android消息机制原理深入分析

    Android消息机制原理深入分析

    这篇文章主要介绍了Android消息机制原理,Android的消息机制主要是指Handler的运行机制以及Handler所附带的MessageQueue和Looper的工作过程
    2022-12-12
  • Android 混淆代码详解及实例

    Android 混淆代码详解及实例

    本文主要介绍Android 混淆代码的资料,这里整理了详细资料及代码实例,有需要做Android 混淆代码的朋友可以参考下
    2016-09-09
  • Android开发环境搭建过程图文详解

    Android开发环境搭建过程图文详解

    这篇文章主要介绍了Android开发环境搭建过程图文详解,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • Android NDK开发(C语言--动态内存分配)

    Android NDK开发(C语言--动态内存分配)

    这篇文章主要介绍了Android NDK开发 C语言--动态内存分配
    2021-12-12
  • Android获取应用程序名称(ApplicationName)示例

    Android获取应用程序名称(ApplicationName)示例

    本文以实例方式为大家介绍下获取应用程序名称(ApplicationName)的具体实现,感兴趣的各位可以参考下哈
    2013-06-06
  • Android时间对话框使用方法详解

    Android时间对话框使用方法详解

    这篇文章主要为大家详细介绍了Android时间对话框的使用方法,包括analogclock和digitalclock显示时钟的控件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • Android自定义方框EditText注册验证码

    Android自定义方框EditText注册验证码

    这篇文章主要为大家详细介绍了Android自定义方框EditText注册验证码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • Android Studio ADB网络调试汇总

    Android Studio ADB网络调试汇总

    这篇文章主要为大家详细介绍了Android Studio ADB网络调试的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05

最新评论