这里我们给大家总结了下关于Android TextView文本文字的常用两种应用,一种是像我们使用微信会看到长文件是可以折叠显示了,还有一种就是TextView文字颜色TextColor焦点效果,下面我一起来看这两种方法。

textview文字状态一,TextView文字颜色TextColor焦点效果

代码如下

<TextView

android:id="@+id/tv_quit"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="@drawable/list_item_color" />

list_item_color 文件

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- 单击选中时字体颜色-->

<item android:state_pressed="true" android:color="#FFFFFF" />

<!-- 有焦点时的字体颜色-->

<item android:state_focused="true" android:color="#FFFFFF" />

<!-- 滚动选中时字体颜色-->

<item android:state_selected="true" android:color="#FFFFFF" />

<!-- 默认字体颜色-->

<item android:color="#000000" />

</selector>

textview文字状态二,Android TextView文本折叠效果

本例要实现文本展开收起的效果,即默认只显示4行文字,如果textview文字超过4行的话,点击右下角的 更多 按钮即可查看全部的内容。之前的做法是根据 TextView 中的字数来判断,效果不太好。这里在一个FrameLayout 包裹两个 TextView

布局文件 activity_main.xml

代码如下    复制代码

<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"

android:padding="10dp"

tools:context=".MainActivity" >

<”http://www.maiziedu.com”=RelativeLayout xmlns:android>

<TextView

android:id="@+id/tv_title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_marginTop="10dp"

android:layout_marginBottom="10dp"

android:text="@string/textview_fold" />

<FrameLayout

android:id="@+id/fl_desc"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_below="@id/tv_title"

android:fadingEdge="horizontal"

android:fadingEdgeLength="5dp" >

<TextView

android:id="@+id/tv_desc_short"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:maxLines="4"

android:textColor="@color/black"

android:textSize="16sp" />

<TextView

android:id="@+id/tv_desc_long"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textColor="@color/black"

android:textSize="16sp" />

</FrameLayout>

<Button

android:id="@+id/bt_more"

android:layout_width="50dp"

android:layout_height="25dp"

android:layout_alignParentRight="true"

android:layout_below="@id/fl_desc"

android:layout_marginRight="10dp"

android:background="#1c000000"

android:gravity="center"

android:text="@string/label_more"

android:textSize="15sp"

android:visibility="gone" />

<ImageView

android:id="@+id/iv_more_line"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignBaseline="@id/bt_more"

android:layout_below="@id/fl_desc"

android:layout_toLeftOf="@id/bt_more"

android:background="@drawable/more_line"

android:contentDescription="@string/app_name"

android:visibility="gone" />

</RelativeLayout>

MainActivity.java

代码如下

package com.example.textviewfold;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewTreeObserver;

import android.view.ViewTreeObserver.OnPreDrawListener;

import android.widget.Button;

import android.widget.FrameLayout;

import android.widget.ImageView;

import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

private Button bt_more;

private FrameLayout fl_desc;

private TextView tv_desc_short;

private TextView tv_desc_long;

private boolean isInit = false;

private boolean isShowShortText = true;

private ImageView iv_more_line;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

findView();

initView();

setListener();

}

private void setListener() {

bt_more.setOnClickListener(this);

}

private void initView() {

String content = " 新浪科技讯 北京时间7月25日凌晨消息,在今天举行的新产品发布会上,谷歌发布Android 4.3版本,代号仍为"果冻豆 (Jelly Bean)"。IT在线教育平台麦子学院今天发布的新一代Nexus 7将搭载该操作系统,Nexus系列设备今日可收到OTA推送更新。 Android 4.3操作系统新增一系列功能。首先是多用户设置功能,包括针对保护儿童的“受限文件(restricted profiles)” 特性。用户可以对应用内容进行限制,防止儿童在使用应用时看到不适宜内容,或接触不合适的应用内购买广告。这项功能与微软Windows Phone 的"儿童乐园(Microsoft's Kid's Corner)"功能类似。第二项升级是智能蓝牙(Bluetooth Smart)功 能,即"低功耗蓝牙(Bluetooth Low Energy)"。";

tv_desc_short.setText(content);

tv_desc_long.setText(content);

ViewTreeObserver vto = fl_desc.getViewTreeObserver();

vto.addOnPreDrawListener(new OnPreDrawListener() {

@Override

public boolean onPreDraw() {

if (isInit)

return true;

if (mesureDescription(tv_desc_short, tv_desc_long)) {

iv_more_line.setVisibility(View.VISIBLE);

bt_more.setVisibility(View.VISIBLE);

}

isInit = true;

return true;

}

});

}

/**

* 计算描述信息是否过长

*/

private boolean mesureDescription(TextView shortView, TextView longView) {

final int shortHeight = shortView.getHeight();

final int longHeight = longView.getHeight();

if (longHeight > shortHeight) {

shortView.setVisibility(View.VISIBLE);

longView.setVisibility(View.GONE);

return true;

}

shortView.setVisibility(View.GONE);

longView.setVisibility(View.VISIBLE);

return false;

}

private void findView() {

fl_desc = (FrameLayout) findViewById(R.id.fl_desc);

tv_desc_short = (TextView) findViewById(R.id.tv_desc_short);

tv_desc_long = (TextView) findViewById(R.id.tv_desc_long);

bt_more = (Button) findViewById(R.id.bt_more);

iv_more_line = (ImageView) findViewById(R.id.iv_more_line);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.bt_more:

if (isShowShortText) {

tv_desc_short.setVisibility(View.GONE);

tv_desc_long.setVisibility(View.VISIBLE);

} else {

tv_desc_short.setVisibility(View.VISIBLE);

tv_desc_long.setVisibility(View.GONE);

}

toogleMoreButton(bt_more);

isShowShortText = !isShowShortText;

break;

default:

break;

}

}

/**

* 更改按钮【更多】的文本

*/

private void toogleMoreButton(Button btn) {

String text = (String) btn.getText();

String moreText = getString(R.string.label_more);

String lessText = getString(R.string.label_less);

if (moreText.equals(text)) {

btn.setText(lessText);

} else {

btn.setText(moreText);

}

}

}

运行效果

Android TestView文本文字修改实例的更多相关文章

  1. android从Dialog对话框中取得文本文字

    android中Dialog对话框获取文本文字,只需要使用editor的getText方法就可以获得,示例如下:final EditText et = new EditText(this); et.s ...

  2. Android技术分享-文字转语音并朗读

    Android技术分享-文字转语音并朗读 最近在做一个项目,其中有一个功能是需要将文本转换成语音并播放出来.下面我将我的做法分享一下. 非常令人开心的是,Android系统目前已经集成了TTS,提供了 ...

  3. 怎么在PDF上进行文字修改

    文件相信大家不论是工作中还是在学习生活中都会有遇到,有时候我们会遇到PDF文件中的文字有时候会有错误的时候,这个时候就需要对修改PDF文件上的文字,那么具体要怎么做呢,PDF文件需要借助软件才可以编辑 ...

  4. Android Launcher分析和修改10——HotSeat深入进阶

    前面已经写过Hotseat分析的文章,主要是讲解如何在Launcher里面配置以及修改Hotseat的参数.今天主要是讲解一下如何在Hotseat里面的Item显示名称.这个小问题昨天折腾了半天,最后 ...

  5. Android Launcher分析和修改3——Launcher启动和初始化

    前面两篇文章都是写有关Launcher配置文件的修改,代码方面涉及不多,今天开始进入Launcher代码分析. 我们开机启动Launcher,Launcher是由Activity Manager启动的 ...

  6. Android - 富文本编辑器

    Android富文本编辑器(一):基础知识 目前主流的基于Android富文本开发方式思路如下: 基于TextView图文混排 使用方式: TextView textView = new TextVi ...

  7. (转)完美解决 Android WebView 文本框获取焦点后自动放大有关问题

    完美解决 Android WebView 文本框获取焦点后自动放大问题 前几天在写一个项目时,要求在项目中嵌入一个WebView 本来很快就完成了,测试也没有问题.但发给新加坡时,他们测试都会出现文本 ...

  8. PHP给图片加上图片水印和文字水印实例

    下面给大家分享一下PHP给图片加上图片水印和文字水印实例,这也是网站经常用到的功能,把代码加上去,调用就很简单了. 核心代码: function imageWaterMark($groundImage ...

  9. Android TextView文本处理库推荐

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/115 Android TextView文本处理库推荐 现在 ...

随机推荐

  1. Python基础03 序列

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! sequence 序列 sequence(序列)是一组有顺序的元素的集合 (严格的 ...

  2. 客户端获取服务端自定义类数据 z

    客户端获取服务端自定义类数据 问题一:超时问题,在最后获取数据的时候突然提示服务超时,服务已断开 解决:配置文件添加: <bindings> <wsHttpBinding> & ...

  3. 03.product.js

    /* item.jd.com Compressed by uglify Author:keelii Date: 2014-08-05 6:52:26 [PM] */ function insertSc ...

  4. rand()和srand()区别

    标准库<cstdlib>提供两个帮助生成伪随机数的函数: 函数一:int rand(void):从srand (seed)中指定的seed开始,返回一个[seed, RAND_MAX(0x ...

  5. shell local

    Shell函数定义的变量默认是global的,其作用域从"函数被调用时执行变量定义的地方"开始,到shell结束 http://blog.chinaunix.net/xmlrpc. ...

  6. eclipse设置svn代理

    共2个步骤: 1. 找到C:\Documents and Settings\用户名\Application Data\Subversion的servers文件, 将#http-proxy-host和# ...

  7. LeetCode 107. Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  8. [HDU 4747] Mex (线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 这道题是我去年刚入校队的时候参加网赛的题目. 一年过去了,我依然还是不会做.. 这是我难题计划的 ...

  9. Android系统下的动态Dex加载

    1 问题在Android系统中,一个App的所有代码都在一个Dex文件里面.Dex是一个类似Jar的存储了多有Java编译字节码的归档文件.因为Android系统使用Dalvik虚拟机,所以需要把使用 ...

  10. 剑指Offer:面试题6——重建二叉树(java实现)

    问题描述:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不包含重复的数字. 例如: 输入:前序{1,2,4,7,3,5,6,8},中序{4,7,2,1 ...