http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 2086|回复: 3

[新手开发之旅] Android新手开发之旅-UI控件之TextView

[复制链接]
发表于 2018-12-1 12:50:32 | 显示全部楼层 |阅读模式
本帖最后由 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>

效果图:

               QQ截图20181201111050.png

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的背景

                      QQ截图20181201114802.png


QQ截图20181201115222.png

示例代码:
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>

效果图:

QQ截图20181201115456.png


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>

效果图:

QQ截图20181201122658.png


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>



效果图下图:

QQ截图20181201123929.png


还有些功能没说到,之后会补上




                        





1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2018-12-1 12:53:05 | 显示全部楼层
强烈支持楼主ing……
发表于 2018-12-2 16:04:46 | 显示全部楼层
安卓开发跟前端还是有点相似的,看来也容易学啊
发表于 2018-12-2 17:22:57 | 显示全部楼层
看到这帖子真是高兴!
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-25 17:40

© 2014-2021

快速回复 返回顶部 返回列表